curves-1.1.0.1: Library for drawing curve based images.

Graphics.Curves.Math

Description

Simple two-dimensional linear algebra.

Synopsis

# Vectors

data Vec Source

Two-dimensional vectors.

Constructors

 Vec FieldsgetX :: !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 = Vec 1 0`
`unitY = Vec 0 1`
`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.

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.

The weighted average of two points.

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

# Line segments

data Segment Source

Constructors

 Seg FieldsgetStart :: !Point getEnd :: !Point

Instances

 Eq Segment Ord Segment Show Segment Transformable Segment DistanceToPoint Segment

The length of a segment.

``` segmentLength (Seg p q) = distance p q
```

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

Is a point to the left of a line segment, as seen from the start of the segment looking a the end?

Compute the intersection point of two segments, if any.

Compute the intersection point of two lines, if any.

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

# Basis

data Basis Source

A basis for a coordinate system.

Constructors

 Basis Fieldsorigin :: Point xUnit :: Point yUnit :: Point

Instances

 Eq Basis Ord Basis Show Basis Transformable Basis
``` defaultBasis = Basis 0 unitX unitY
```

Translate a point from the `defaultBasis` to the given basis.

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

Instances

 DistanceToPoint Segment DistanceToPoint Vec

# Transformations

class Transformable a whereSource

Methods

transform :: (Point -> Point) -> a -> aSource

Apply a transformation to all points in an object.

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

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.