Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
Graphics.Curves.Math
Description
Simple two-dimensional linear algebra.
- type Scalar = Double
- data Vec = Vec {}
- type Point = Vec
- unitX :: Vec
- unitY :: Vec
- diag :: Scalar -> Vec
- vmap :: (Scalar -> Scalar) -> Vec -> Vec
- vzip :: (Scalar -> Scalar -> Scalar) -> Vec -> Vec -> Vec
- vuncurry :: (Scalar -> Scalar -> a) -> Vec -> a
- vcurry :: (Vec -> a) -> Scalar -> Scalar -> a
- dot :: Vec -> Vec -> Scalar
- rot90 :: Vec -> Vec
- norm :: Vec -> Vec
- angle :: Vec -> Vec -> Scalar
- interpolate :: Point -> Point -> Scalar -> Point
- data Segment = Seg {}
- segmentLength :: Segment -> Scalar
- squareSegmentLength :: Segment -> Scalar
- leftOf :: Point -> Segment -> Bool
- intersectSegment :: Segment -> Segment -> Maybe Point
- intersectLine :: Segment -> Segment -> Maybe Point
- intersectLineSegment :: Segment -> Segment -> Maybe Point
- data Basis = Basis {}
- defaultBasis :: Basis
- toBasis :: Basis -> Point -> Point
- fromBasis :: Basis -> Point -> Point
- class DistanceToPoint a where
- class Transformable a where
- translate :: Transformable a => Vec -> a -> a
- scale :: Transformable a => Vec -> a -> a
- scaleFrom :: Transformable a => Point -> Vec -> a -> a
- rotate :: Transformable a => Scalar -> a -> a
- rotateAround :: Transformable a => Point -> Scalar -> a -> a
- findThreshold :: (Scalar -> a) -> (a -> Bool) -> Scalar -> Scalar -> Scalar -> Maybe (Scalar, a)
Vectors
data Vec
Two-dimensional vectors.
vzip :: (Scalar -> Scalar -> Scalar) -> Vec -> Vec -> Vec
Point-wise lifting of an operator on coordinates.
interpolate :: Point -> Point -> Scalar -> Point
The weighted average of two points.
interpolate p q t == (1 - t) * p + t * q
Line segments
data Segment
Instances
segmentLength :: Segment -> Scalar
The length of a segment.
segmentLength (Seg p q) = distance p q
squareSegmentLength :: Segment -> Scalar
The square length of a segment. Avoids computing a square root.
leftOf :: Point -> Segment -> Bool
Is a point to the left of a line segment, as seen from the start of the segment looking a the end?
intersectSegment :: Segment -> Segment -> Maybe Point
Compute the intersection point of two segments, if any.
intersectLine :: Segment -> Segment -> Maybe Point
Compute the intersection point of two lines, if any.
intersectLineSegment :: Segment -> Segment -> Maybe Point
Compute the intersection point of a line and a segment, if any.
Basis
data Basis
A basis for a coordinate system.
defaultBasis = Basis 0 unitX unitY
toBasis :: Basis -> Point -> Point
Translate a point from the defaultBasis
to the given basis.
fromBasis :: Basis -> Point -> Point
Translate a point in the given basis to the defaultBasis
.
Distances
class DistanceToPoint a where
Minimal complete definition
Nothing
Methods
distance :: a -> Point -> Scalar
Compute the distance from an a
to a given point. Default implementation:
distance x p = sqrt (squareDistance x p)
squareDistance :: a -> Point -> Scalar
The square of the distance from an a
to a point. Default implementation:
squareDistance x p = distance x p ^ 2
distanceAtMost :: Scalar -> a -> Point -> Maybe Scalar
The distance from an a
to a point if it's less than a given value.
distanceAtMost d x p == Nothing
implies that distance x p > d
.
Instances
Transformations
class Transformable a where
Instances
Transformable () | |
Transformable Basis | |
Transformable Segment | |
Transformable Vec | |
Transformable Image | |
Transformable a => Transformable [a] | |
Transformable a => Transformable (Maybe a) | |
Transformable b => Transformable (a -> b) | |
(Transformable a, Transformable b) => Transformable (Either a b) | |
(Transformable a, Transformable b) => Transformable (a, b) | |
(Transformable a, Transformable b, Transformable c) => Transformable (a, b, c) |
translate :: Transformable a => Vec -> a -> a
translate v = transform (+ v)
scale :: Transformable a => Vec -> a -> a
scale v = transform (* v)
scaleFrom :: Transformable a => Point -> Vec -> a -> a
Scale using a given point as the center.
rotate :: Transformable a => Scalar -> a -> a
Rotate an object counterclockwise around the origin.
rotateAround :: Transformable a => Point -> Scalar -> a -> a
Rotate an object counterclockwise around a given point.
Function analysis
Arguments
:: (Scalar -> a) | The function to analyze. |
-> (a -> Bool) | The predicate. |
-> Scalar | Precision. The actual smallest value will be less than this much smaller than the returned value. |
-> Scalar | A minimum value. No solution smaller than this will be returned. |
-> Scalar | A maximum value. If no solution is found
below this value, |
-> Maybe (Scalar, a) |
Find the smallest value making a function satisfy a given predicate. Needs the function to be monotone in the predicate to work properly.