Module

Linear.Affine

Package
purescript-linear
Repository
afcondon/purescript-linear

Affine spaces and points.

This module provides the Affine typeclass for affine spaces, where points and vectors are distinguished at the type level. Points can be subtracted to yield vectors, and vectors can be added to points, but points cannot be added to each other.

#Point Source

newtype Point :: forall k. (k -> Type) -> k -> Typenewtype Point f a

A point in an affine space.

The Point newtype distinguishes points from vectors at the type level. This prevents nonsensical operations like adding two points together.

origin = P (V2 0.0 0.0)
position = P (V2 3.0 4.0)

Constructors

  • P (f a)

Instances

#unP Source

unP :: forall f a. Point f a -> f a

Unwrap a point to get the underlying vector.

#Affine Source

class Affine :: (Type -> Type) -> (Type -> Type) -> Constraintclass (Additive d) <= Affine p d | p -> d where

An affine space is a set of points with associated difference vectors.

Laws:

  • p .+^ (q .-. p) = q
  • (p .+^ u) .+^ v = p .+^ (u ^+^ v)
  • p .-. p = zero

Members

  • diff :: forall a. Ring a => p a -> p a -> d a

    The vector from the first point to the second.

  • moveBy :: forall a. Semiring a => p a -> d a -> p a

    Add a vector to a point.

  • moveByNeg :: forall a. Ring a => p a -> d a -> p a

    Subtract a vector from a point.

Instances

#(.-.) Source

Operator alias for Linear.Affine.diff (left-associative / precedence 6)

#(.+^) Source

Operator alias for Linear.Affine.moveBy (left-associative / precedence 6)

#(.-^) Source

Operator alias for Linear.Affine.moveByNeg (left-associative / precedence 6)

#origin Source

origin :: forall f a. Applicative f => Semiring a => Point f a

The origin point (all coordinates zero).

origin :: Point V3 Number
origin = P (V3 0.0 0.0 0.0)

#distanceA Source

distanceA :: forall p d. Affine p d => Metric d => p Number -> p Number -> Number

The distance between two points.

distanceA (P (V2 0.0 0.0)) (P (V2 3.0 4.0)) = 5.0

#qdA Source

qdA :: forall p d. Affine p d => Metric d => p Number -> p Number -> Number

The squared distance between two points.

More efficient than distanceA when you only need to compare distances.

qdA (P (V2 0.0 0.0)) (P (V2 3.0 4.0)) = 25.0

#lerpP Source

lerpP :: forall p d. Affine p d => Functor d => Number -> p Number -> p Number -> p Number

Linear interpolation between two points.

lerpP 0.0 p1 p2 = p1
lerpP 1.0 p1 p2 = p2
lerpP 0.5 p1 p2 -- midpoint