curves-1.1.0.1: Library for drawing curve based images.

Safe HaskellSafe-Inferred

Graphics.Curves.Math

Contents

Description

Simple two-dimensional linear algebra.

Synopsis

Vectors

data Vec Source

Two-dimensional vectors.

Constructors

Vec 

Fields

getX :: !Scalar
 
getY :: !Scalar
 

Instances

Eq Vec 
Fractional Vec 
Num Vec

Numbers are lifted to vectors using diag. Arithmetic operations apply point-wise.

Ord Vec 
Show Vec 
Arbitrary Vec 
Transformable Vec 
DistanceToPoint Vec 

unitX :: VecSource

unitX = Vec 1 0

unitY :: VecSource

unitY = Vec 0 1

diag :: Scalar -> VecSource

diag x = Vec x x

vmap :: (Scalar -> Scalar) -> Vec -> VecSource

Apply a function to the coordinates of a vector.

vzip :: (Scalar -> Scalar -> Scalar) -> Vec -> Vec -> VecSource

Point-wise lifting of an operator on coordinates.

vuncurry :: (Scalar -> Scalar -> a) -> Vec -> aSource

vuncurry f (Vec x y) = f x y

vcurry :: (Vec -> a) -> Scalar -> Scalar -> aSource

vcurry f x y = f (Vec x y)

dot :: Vec -> Vec -> ScalarSource

The dot product of two vectors.

rot90 :: Vec -> VecSource

Rotate a vector 90 degrees counterclockwise.

norm :: Vec -> VecSource

Normalize a vector.

 norm v = v / abs v

angle :: Vec -> Vec -> ScalarSource

The counterclockwise angle between two vectors.

interpolate :: Point -> Point -> Scalar -> PointSource

The weighted average of two points.

 interpolate p q t == (1 - t) * p + t * q

Line segments

segmentLength :: Segment -> ScalarSource

The length of a segment.

 segmentLength (Seg p q) = distance p q

squareSegmentLength :: Segment -> ScalarSource

The square length of a segment. Avoids computing a square root.

leftOf :: Point -> Segment -> BoolSource

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 PointSource

Compute the intersection point of two segments, if any.

intersectLine :: Segment -> Segment -> Maybe PointSource

Compute the intersection point of two lines, if any.

intersectLineSegment :: Segment -> Segment -> Maybe PointSource

Compute the intersection point of a line and a segment, if any.

Basis

data Basis Source

A basis for a coordinate system.

Constructors

Basis 

Fields

origin :: Point
 
xUnit :: Point
 
yUnit :: Point
 

defaultBasis :: BasisSource

 defaultBasis = Basis 0 unitX unitY

toBasis :: Basis -> Point -> PointSource

Translate a point from the defaultBasis to the given basis.

fromBasis :: Basis -> Point -> PointSource

Translate a point in the given basis to the defaultBasis.

Distances

class DistanceToPoint a whereSource

Methods

distance :: a -> Point -> ScalarSource

Compute the distance from an a to a given point. Default implementation:

 distance x p = sqrt (squareDistance x p)

squareDistance :: a -> Point -> ScalarSource

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 ScalarSource

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.

Transformations

translate :: Transformable a => Vec -> a -> aSource

 translate v = transform (+ v)

scale :: Transformable a => Vec -> a -> aSource

 scale v = transform (* v)

scaleFrom :: Transformable a => Point -> Vec -> a -> aSource

Scale using a given point as the center.

rotate :: Transformable a => Scalar -> a -> aSource

Rotate an object counterclockwise around the origin.

rotateAround :: Transformable a => Point -> Scalar -> a -> aSource

Rotate an object counterclockwise around a given point.

Function analysis

findThresholdSource

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, Nothing is returned.

-> 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.