SG-1.0: Small geometry library for dealing with vectors and collision detection

Data.SG.Geometry

Description

This module has the type-class (and associated functions) for dealing with geometric systems of 2 or 3 dimensions.

Synopsis

Documentation

class (VectorNum rel, Coord rel, Coord pt, IsomorphicVectors rel pt, IsomorphicVectors pt rel) => Geometry rel pt ln | rel -> pt ln, pt -> rel ln, ln -> rel pt whereSource

A geometry system, parameterised over points, relative (free) vectors, and lines. There are separate instances for two dimensions and for three dimensions. Each pair of type-class parameters is uniquely determined by the other parameter (i.e. by the dimensionality, and which vector type you are using).

Minimal implementation: everything but scaleRel.

Methods

scaleRel :: Num a => a -> rel a -> rel aSource

Scales a relative (free) vector by the given amount.

plusDir :: Num a => pt a -> rel a -> pt aSource

Adds a relative (free) vector to a given point.

fromPt :: Num a => pt a -> pt a -> rel aSource

Determines the relative (free) vector to the first parameter from the second parameter. So:

 Point2 (1,8) `fromPt` Point2 (3,4) == Point2 (-2,3)

getLineVecs :: Num a => ln a -> (pt a, rel a)Source

Given a line, converts it back into its point and relative vector. It should always be the case that uncurry makeLine . getLineVecs is the identity function.

makeLine :: Num a => pt a -> rel a -> ln aSource

Given a point and relative vector, creates a line. It should always be the case that uncurry makeLine . getLineVecs is the identity function.

minusDir :: (Num a, Geometry rel pt ln) => pt a -> rel a -> pt aSource

Adds the negation of the relative (free) vector to the point.

toPt :: (Geometry rel pt ln, Num a) => pt a -> pt a -> rel aSource

The flipped version of fromPt.

lineTo :: (Num a, Geometry rel pt ln) => pt a -> pt a -> ln aSource

Gets the line from the first point, to the second point.

lineFrom :: (Num a, Geometry rel pt ln) => pt a -> pt a -> ln aSource

The flipped version of lineTo.

getLineStart :: (Num a, Geometry rel pt ln) => ln a -> pt aSource

Gets the point at the start of the line.

getLineDir :: (Num a, Geometry rel pt ln) => ln a -> rel aSource

Gets the direction vector of the line.

getLineEnd :: (Geometry rel pt ln, Num a) => ln a -> pt aSource

Gets the point at the end of the line.

makeLength :: (Floating a, Ord a, Geometry rel pt ln) => a -> ln a -> ln aSource

Alters the line to the given length, but with the same start point and direction.

alongLine :: (Num a, Geometry rel pt ln) => a -> ln a -> pt aSource

Given a multiple of the direction vector (this is not distance unless the direction vector is a unit vector), calculates that point.

distAlongLine :: (Geometry rel pt ln, Ord a, Floating a) => pt a -> ln a -> Maybe aSource

Checks if the given point is on the given line (to within a small epsilon-tolerance). If it is, gives back the distance along the line (as a multiple of its direction vector) to the point in a Just wrapper. If the point is not on the line, Nothing is returned.

isOnLine :: (Geometry rel pt ln, Ord a, Floating a) => pt a -> ln a -> BoolSource

Checks if the given point is on the given line (to within a small epsilon-tolerance).

nearestDistOnLine :: (Geometry rel pt ln, Ord a, Floating a) => pt a -> ln a -> aSource

Finds the nearest point on the line to the given point, and gives back its distance along the line (as a multiple of the direction vector). Since the nearest distance will be at a right-angle to the point, this is the same as projecting the point onto the line.

nearestPointOnLine :: (Geometry rel pt ln, Ord a, Floating a) => pt a -> ln a -> pt aSource

Finds the nearest point on the line to the given point, and gives back the point.

valueAtX :: (Geometry rel pt ln, Coord2 rel, Coord2 pt, Fractional a) => ln a -> a -> Maybe aSource

Gives the distance along the line (2D or 3D) at a given X value. Returns Nothing if the line is parallel to the YZ plane (in 2D, if the X component of the line is zero). The value returned is a multiple of the direction vector of the line, which will only be the same as distance if the direction vector is a unit vector.

valueAtY :: (Geometry rel pt ln, Coord2 rel, Coord2 pt, Fractional a) => ln a -> a -> Maybe aSource

Gives the distance along the line (2D or 3D) at a given Y value. Returns Nothing if the line is parallel to the XZ plane (in 2D, if the Y component of the line is zero). The value returned is a multiple of the direction vector of the line, which will only be the same as distance if the direction vector is a unit vector.

valueAtZ :: (Geometry rel pt ln, Coord3 rel, Coord3 pt, Fractional a) => ln a -> a -> Maybe aSource

Gives the distance along the 3D line at a given Z value. Returns Nothing if the line is parallel to the XY plane. The value returned is a multiple of the direction vector of the line, which will only be the same as distance if the direction vector is a unit vector.

pointAtX, pointAtY :: (Geometry rel pt ln, Coord2 rel, Coord2 pt, Fractional a) => ln a -> a -> Maybe (pt a)Source

pointAtX (and the Y and Z equivalents) are wrappers around valueAtX (and similar) that give back the point rather than distance along the line.

pointAtZ :: (Geometry rel pt ln, Coord3 rel, Coord3 pt, Fractional a) => ln a -> a -> Maybe (pt a)Source