curves-1.1.0.2: Library for drawing curve based images.

Safe HaskellSafe
LanguageHaskell98

Graphics.Curves.Math

Contents

Description

Simple two-dimensional linear algebra.

Synopsis

Vectors

data Vec Source

Two-dimensional vectors.

Constructors

Vec 

Fields

getX, getY :: !Scalar
 

Instances

Eq Vec Source 
Fractional Vec Source 
Num Vec Source

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

Ord Vec Source 
Show Vec Source 
Transformable Vec Source 
DistanceToPoint Vec Source 

unitX :: Vec Source

unitX = Vec 1 0

unitY :: Vec Source

unitY = Vec 0 1

diag :: Scalar -> Vec Source

diag x = Vec x x

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

Apply a function to the coordinates of a vector.

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

Point-wise lifting of an operator on coordinates.

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

vuncurry f (Vec x y) = f x y

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

vcurry f x y = f (Vec x y)

dot :: Vec -> Vec -> Scalar Source

The dot product of two vectors.

rot90 :: Vec -> Vec Source

Rotate a vector 90 degrees counterclockwise.

norm :: Vec -> Vec Source

Normalize a vector.

norm v = v / abs v

angle :: Vec -> Vec -> Scalar Source

The counterclockwise angle between two vectors.

interpolate :: Point -> Point -> Scalar -> Point Source

The weighted average of two points.

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

Line segments

segmentLength :: Segment -> Scalar Source

The length of a segment.

segmentLength (Seg p q) = distance p q

squareSegmentLength :: Segment -> Scalar Source

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

leftOf :: Point -> Segment -> Bool Source

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 Source

Compute the intersection point of two segments, if any.

intersectLine :: Segment -> Segment -> Maybe Point Source

Compute the intersection point of two lines, if any.

intersectLineSegment :: Segment -> Segment -> Maybe Point Source

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, xUnit, yUnit :: Point
 

defaultBasis :: Basis Source

defaultBasis = Basis 0 unitX unitY

toBasis :: Basis -> Point -> Point Source

Translate a point from the defaultBasis to the given basis.

fromBasis :: Basis -> Point -> Point Source

Translate a point in the given basis to the defaultBasis.

Distances

class DistanceToPoint a where Source

Minimal complete definition

Nothing

Methods

distance :: a -> Point -> Scalar Source

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

distance x p = sqrt (squareDistance x p)

squareDistance :: a -> Point -> Scalar Source

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 Source

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 -> a Source

translate v = transform (+ v)

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

scale v = transform (* v)

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

Scale using a given point as the center.

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

Rotate an object counterclockwise around the origin.

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

Rotate an object counterclockwise around a given point.

Function analysis

findThreshold Source

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.