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

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:

• magSq and mag (defined for all vectors).
• getX and getY (defined for all vectors) as well as getZ (defined for all vectors with 3 or more dimensions).
• dotProduct, unitVector, averageVec, averageUnitVec, sameDirection, projectOnto, projectPointOnto, distFrom (defined for all vectors).
• iso, which is defined for all combinations of vectors with the same number of dimensions.

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 #

Instances

 Source # Methodsiso :: Num a => v a -> v a Source # Source # Methodsiso :: Num a => Triple a -> Rel3' a Source # Source # Methodsiso :: Num a => Triple a -> Point3' a Source # Source # Methodsiso :: Num a => Pair a -> Rel2' a Source # Source # Methodsiso :: Num a => Pair a -> Point2' a Source # Source # Methodsiso :: Num a => Rel2' a -> Pair a Source # Source # Methodsiso :: Num a => Rel2' a -> Point2' a Source # Source # Methodsiso :: Num a => Point2' a -> Pair a Source # Source # Methodsiso :: Num a => Point2' a -> Rel2' a Source # Source # Methodsiso :: Num a => Rel3' a -> Triple a Source # Source # Methodsiso :: Num a => Rel3' a -> Point3' a Source # Source # Methodsiso :: Num a => Point3' a -> Triple a Source # Source # Methodsiso :: Num a => Point3' a -> Rel3' 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

 Source # MethodsgetComponents :: 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 # Source # MethodsgetComponents :: 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 # Source # MethodsgetComponents :: 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 # Source # MethodsgetComponents :: 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 # Source # MethodsgetComponents :: 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 # Source # MethodsgetComponents :: 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 # Source # MethodsgetComponents :: 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

Methods

getX :: p a -> a Source #

getY :: p a -> a Source #

Instances

 Source # MethodsgetX :: Quad a -> a Source #getY :: Quad a -> a Source # Source # MethodsgetX :: Triple a -> a Source #getY :: Triple a -> a Source # Source # MethodsgetX :: Pair a -> a Source #getY :: Pair a -> a Source # Source # MethodsgetX :: Rel2' a -> a Source #getY :: Rel2' a -> a Source # Source # MethodsgetX :: Point2' a -> a Source #getY :: Point2' a -> a Source # Source # MethodsgetX :: Rel3' a -> a Source #getY :: Rel3' a -> a Source # Source # MethodsgetX :: 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

 Source # MethodsgetZ :: Quad a -> a Source # Source # MethodsgetZ :: Triple a -> a Source # Source # MethodsgetZ :: Rel3' a -> a Source # Source # MethodsgetZ :: 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

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

 Source # MethodsfmapNum1 :: 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 # Source # MethodsfmapNum1 :: 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 # Source # MethodsfmapNum1 :: 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 # Source # MethodsfmapNum1 :: 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 # Source # MethodsfmapNum1 :: 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 # Source # MethodsfmapNum1 :: 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 # Source # MethodsfmapNum1 :: 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 #