SGplus-1.1: (updated) Small geometry library for dealing with vectors and collision detection

Safe HaskellSafe
LanguageHaskell98

Data.SG.Vector

Description

The module with all the different type-classes for vectors. Generally, the main functions you might need from this function are:

The rest of the functions are mainly just wiring necessary for other functions, but must be exported.

As to the vector types, there are two methods to use this library. One is to use the types from the Data.SG.Vector.Basic library, which support basic vector operations. The other is to use the types from the Data.SG.Geometry.TwoDim and Data.SG.Geometry.ThreeDim modules, where a position vector is differentiated from a relative vector (to increase clarity of code, and help prevent errors such as adding two points together). Both systems can be used with various useful functions (involving lines too) from Data.SG.Geometry.

Synopsis

Documentation

class IsomorphicVectors from to where Source #

An isomorphism amongst vectors. Allows you to convert between two vectors that have the same dimensions. You will notice that all the instances reflect this.

Minimal complete definition

iso

Methods

iso :: Num a => from a -> to a Source #

class Foldable p => Coord p where Source #

The class that is implemented by all vectors.

Minimal implementation: fromComponents

Minimal complete definition

fromComponents

Methods

getComponents :: Num a => p a -> [a] Source #

Gets the components of the vector, in the order x, y (, z).

fromComponents :: Num a => [a] -> p a Source #

Re-constructs a vector from the list of coordinates. If there are too few, the rest will be filled with zeroes. If there are too many, the latter ones are ignored.

magSq :: Num a => p a -> a Source #

Gets the magnitude squared of the vector. This should be fast for repeated calls on Rel2' and Rel3', which cache this value.

dotProduct :: Num a => p a -> p a -> a Source #

Computes the dot product of the two vectors.

Instances

Coord Quad Source # 

Methods

getComponents :: Num a => Quad a -> [a] Source #

fromComponents :: Num a => [a] -> Quad a Source #

magSq :: Num a => Quad a -> a Source #

dotProduct :: Num a => Quad a -> Quad a -> a Source #

Coord Triple Source # 

Methods

getComponents :: Num a => Triple a -> [a] Source #

fromComponents :: Num a => [a] -> Triple a Source #

magSq :: Num a => Triple a -> a Source #

dotProduct :: Num a => Triple a -> Triple a -> a Source #

Coord Pair Source # 

Methods

getComponents :: Num a => Pair a -> [a] Source #

fromComponents :: Num a => [a] -> Pair a Source #

magSq :: Num a => Pair a -> a Source #

dotProduct :: Num a => Pair a -> Pair a -> a Source #

Coord Rel2' Source # 

Methods

getComponents :: Num a => Rel2' a -> [a] Source #

fromComponents :: Num a => [a] -> Rel2' a Source #

magSq :: Num a => Rel2' a -> a Source #

dotProduct :: Num a => Rel2' a -> Rel2' a -> a Source #

Coord Point2' Source # 

Methods

getComponents :: Num a => Point2' a -> [a] Source #

fromComponents :: Num a => [a] -> Point2' a Source #

magSq :: Num a => Point2' a -> a Source #

dotProduct :: Num a => Point2' a -> Point2' a -> a Source #

Coord Rel3' Source # 

Methods

getComponents :: Num a => Rel3' a -> [a] Source #

fromComponents :: Num a => [a] -> Rel3' a Source #

magSq :: Num a => Rel3' a -> a Source #

dotProduct :: Num a => Rel3' a -> Rel3' a -> a Source #

Coord Point3' Source # 

Methods

getComponents :: Num a => Point3' a -> [a] Source #

fromComponents :: Num a => [a] -> Point3' a Source #

magSq :: Num a => Point3' a -> a Source #

dotProduct :: Num a => Point3' a -> Point3' a -> a Source #

class Coord p => Coord2 p where Source #

This class is implemented by all 2D and 3D vectors, so getX gets the X co-ordinate of both 2D and 3D vectors.

Minimal complete definition

getX, getY

Methods

getX :: p a -> a Source #

getY :: p a -> a Source #

Instances

Coord2 Quad Source # 

Methods

getX :: Quad a -> a Source #

getY :: Quad a -> a Source #

Coord2 Triple Source # 

Methods

getX :: Triple a -> a Source #

getY :: Triple a -> a Source #

Coord2 Pair Source # 

Methods

getX :: Pair a -> a Source #

getY :: Pair a -> a Source #

Coord2 Rel2' Source # 

Methods

getX :: Rel2' a -> a Source #

getY :: Rel2' a -> a Source #

Coord2 Point2' Source # 

Methods

getX :: Point2' a -> a Source #

getY :: Point2' a -> a Source #

Coord2 Rel3' Source # 

Methods

getX :: Rel3' a -> a Source #

getY :: Rel3' a -> a Source #

Coord2 Point3' Source # 

Methods

getX :: Point3' a -> a Source #

getY :: Point3' a -> a Source #

class Coord2 p => Coord3 p where Source #

This class is implemented by all 3D vectors. To get the X and Y components, use getX and getY from Coord2.

Minimal complete definition

getZ

Methods

getZ :: p a -> a Source #

Instances

Coord3 Quad Source # 

Methods

getZ :: Quad a -> a Source #

Coord3 Triple Source # 

Methods

getZ :: Triple a -> a Source #

Coord3 Rel3' Source # 

Methods

getZ :: Rel3' a -> a Source #

Coord3 Point3' Source # 

Methods

getZ :: Point3' a -> a Source #

origin :: (Coord p, Num a) => p a Source #

The origin/all-zero vector (can be used with any vector type you like)

mag :: (Coord p, Floating a) => p a -> a Source #

Gets the magnitude of the given vector.

unitVector :: (Coord p, VectorNum p, Ord a, Floating a) => p a -> p a Source #

Scales the vector so that it has length 1. Note that due to floating-point inaccuracies and so on, mag (unitVector v) will not necessarily equal 1, but it should be very close. If an all-zero vector is passed, the same will be returned.

This function should be very fast when called on Rel2' and Rel3'; vectors that are already unit vectors (no processing is done).

averageVec :: (Fractional a, VectorNum p, Num (p a)) => [p a] -> p a Source #

Gets the average vector of all the given vectors. Essentially it is the sum of the vectors, divided by the length, so averageVec [Point2 (-3, 0), Point2 (5,0)] will give Point2 (1,0). If the list is empty, the all-zero vector is returned.

averageUnitVec :: (Floating a, Ord a, Coord p, VectorNum p, Num (p a)) => [p a] -> p a Source #

Like averageVec composed with unitVector -- gets the average of the vectors in the list, and normalises the length. If the list is empty, the all-zero vector is returned (which is therefore not a unit vector). Similarly, if the average of all the vectors is all-zero, the all-zero vector will be returned.

sameDirection :: (VectorNum rel, Coord rel, Ord a, Floating a) => rel a -> rel a -> Bool Source #

Works out if the two vectors are in the same direction (to within a small tolerance).

projectOnto :: (Floating a, Ord a, VectorNum rel, Coord rel) => rel a -> rel a -> a Source #

Gives back the vector (first parameter), translated onto given axis (second parameter). Note that the scale is always distance, not related to the size of the axis vector.

projectOnto2 :: (Floating a, Ord a, VectorNum rel, Coord rel) => rel a -> (rel a, rel a) -> rel a Source #

Projects the first parameter onto the given axes (X, Y), returning a point in terms of the new axes.

projectPointOnto :: (Floating a, Ord a, VectorNum rel, Coord rel, IsomorphicVectors pt rel) => pt a -> rel a -> a Source #

Gives back the point (first parameter), translated onto given axis (second parameter). Note that the scale is always distance, not related to the size of the axis vector.

projectPointOnto2 :: (Floating a, Ord a, VectorNum rel, Coord rel, IsomorphicVectors pt rel, Coord pt) => pt a -> (rel a, rel a) -> pt a Source #

Projects the point (first parameter) onto the given axes (X, Y), returning a point in terms of the new axes.

distFrom :: (VectorNum pt, Coord pt, Floating a) => pt a -> pt a -> a Source #

Works out the distance between two points.

class VectorNum f where Source #

A modified version of Functor and Applicative that adds the Num constraint on the result. You are unlikely to need to use this class much directly. Some vectors have Functor and Applicative instances anyway.

Minimal complete definition

fmapNum1, fmapNum2, fmapNum1inv, simpleVec

Methods

fmapNum1 :: Num b => (a -> b) -> f a -> f b Source #

Like fmap, but with a Num constraint.

fmapNum2 :: Num c => (a -> b -> c) -> f a -> f b -> f c Source #

Like liftA2, but with a Num constraint.

fmapNum1inv :: Num a => (a -> a) -> f a -> f a Source #

Like fmapNum1, but can only be used if you won't change the magnitude:

simpleVec :: Num a => a -> f a Source #

Like pure (or fromInteger) but with a Num constraint.

Instances

VectorNum Quad Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Quad a -> Quad b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Quad a -> Quad b -> Quad c Source #

fmapNum1inv :: Num a => (a -> a) -> Quad a -> Quad a Source #

simpleVec :: Num a => a -> Quad a Source #

VectorNum Triple Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Triple a -> Triple b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Triple a -> Triple b -> Triple c Source #

fmapNum1inv :: Num a => (a -> a) -> Triple a -> Triple a Source #

simpleVec :: Num a => a -> Triple a Source #

VectorNum Pair Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Pair a -> Pair b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Pair a -> Pair b -> Pair c Source #

fmapNum1inv :: Num a => (a -> a) -> Pair a -> Pair a Source #

simpleVec :: Num a => a -> Pair a Source #

VectorNum Rel2' Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Rel2' a -> Rel2' b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Rel2' a -> Rel2' b -> Rel2' c Source #

fmapNum1inv :: Num a => (a -> a) -> Rel2' a -> Rel2' a Source #

simpleVec :: Num a => a -> Rel2' a Source #

VectorNum Point2' Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Point2' a -> Point2' b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Point2' a -> Point2' b -> Point2' c Source #

fmapNum1inv :: Num a => (a -> a) -> Point2' a -> Point2' a Source #

simpleVec :: Num a => a -> Point2' a Source #

VectorNum Rel3' Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Rel3' a -> Rel3' b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Rel3' a -> Rel3' b -> Rel3' c Source #

fmapNum1inv :: Num a => (a -> a) -> Rel3' a -> Rel3' a Source #

simpleVec :: Num a => a -> Rel3' a Source #

VectorNum Point3' Source # 

Methods

fmapNum1 :: Num b => (a -> b) -> Point3' a -> Point3' b Source #

fmapNum2 :: Num c => (a -> b -> c) -> Point3' a -> Point3' b -> Point3' c Source #

fmapNum1inv :: Num a => (a -> a) -> Point3' a -> Point3' a Source #

simpleVec :: Num a => a -> Point3' a Source #