## 2d-primitives

### Description

A collection of primitives and linear algebra common for 2D graphics and 2D games.

### Author

Richard van Roy (pluizer)

### Requirements

none

### Documentation

#### Floating points

*[procedure]*

`(fmod numer denom)`

Returns the floating-point remainder of numer/denom (rounded towards zero).

*[procedure]*

`(clamp f mmin mmax)`

Clamp a floating-point number to a minimum and a maximum.

*[procedure]*

`(sqr x)`

Square a number.

*[constant]*

`(float-min)`

Minimum representable floating-point number.

*[constant]*

`infinity`

#### Constants

*[constant]*

`epsilon`

*[constant]*

`pi`

*[constant]*

`pi/2`

*[constant]*

`2pi`

*[constant]*

`-pi`

*[constant]*

`360/2pi`

*[constant]*

`2pi/360`

#### Angles

*[procedure]*

`(radian->degree radians)`

*[procedure]*

`(degree->radian degrees)`

*[procedure]*

`(flip-radian-h radians)`

*[procedure]*

`(flip-radian-v radians)`

*[procedure]*

`(flip-degree-h degrees)`

*[procedure]*

`(flip-degree-v degrees)`

#### Vectors

*[procedure]*

`(vect:create x y)`

Returs a new vector

*[procedure]*

`(vect? obj)`

*[procedure]*

`(vect:x v)`

*[procedure]*

`(vect:y v)`

*[procedure]*

`(zero-vect)`

Constant for the zero vector.

*[procedure]*

`(vect=? a b #!optional (epsilon .001)`

Check if two vectors are equal.

*[procedure]*

`(vect+ a b)`

Add two vectors.

*[procedure]*

`(vect- a #!optional b)`

Subtract two vectors or negate a vector.

*[procedure]*

`(vect* v s)`

Scalar multiplication.

*[procedure]*

`(vect:dot a b)`

Vector dot product.

*[procedure]*

`(vect:cross a b)`

2D vector cross product analog. The cross product of 2D vectors results in a 3D vector with only a z component. This function returns the magnitude of the z value.

*[procedure]*

`(vect:perp v)`

Returns a perpendicular vector. <procedure>(90 degree rotation)</procedure><br/>

*[procedure]*

`(vect:vperp v)`

Returns a perpendicular vector. <procedure>(-90 degree rotation)</procedure><br/>

*[procedure]*

`(vect:project a b)`

Returns the vector projection of /a/ onto /b/.

*[procedure]*

`(angle->vect a)`

Returns the unit length vector for the given angle (in radians).

*[procedure]*

`(vect->angle v)`

Returns the angular direction v is pointing in (in radians).

*[procedure]*

`(vect:rotate a b)`

Uses complex number multiplication to rotate /a/ by /b/. Scaling will occur if /a/ is not a unit vector.

*[procedure]*

`(vect:unrotate a b)`

Inverse of vect:rotate

*[procedure]*

`(vect:length-squared v)`

Returns the squared length of v. Faster than /(vect:length)/ when you only need to compare lengths.

*[procedure]*

`(vect:length v)`

Returns the length of v.

*[procedure]*

`(vect:lerp v1 v2 t)`

Linearly interpolate between /a/ and /b/.

*[procedure]*

`(vect:normalize v)`

Returns a normalized copy of v.

*[procedure]*

`(vect:clamp v len)`

Clamp v to length len.

*[procedure]*

`(vect:lerp-const v1 v2 dist)`

Linearly interpolate between v1 towards v2 by distance d.

*[procedure]*

`(vect:dist v1 v2)`

Returns the distance between v1 and v2.

*[procedure]*

`(vect:dist-squared v1 v2)`

Returns the squared distance between v1 and v2. Faster than /(vect:distance)/ when you only need to compare distances.

*[procedure]*

`(vect:near? a b dist)`

Returns true if the distance between v1 and v2 is less than dist.

*[procedure]*

`(vect:spherical-lerp a b t)`

Spherical linearly interpolate between /a/ and /b/.

*[procedure]*

`(vect:spherical-lerp-const a b angle)`

Spherical linearly interpolate between /a/ towards /b/ by no more than angle /angle/ in radians.

#### Bounding Boxes

*[procedure]*

`(rect:create l r b t)`

Returs a new bounding box.

*[procedure]*

`(rect? obj)`

Can also be a line.

*[procedure]*

`(rect:l rect)`

*[procedure]*

`(rect:r rect)`

*[procedure]*

`(rect:b rect)`

*[procedure]*

`(rect:t rect)`

*[procedure]*

`(rect:for-circle p r)`

Constructs a /rect/ for a circle with the given position and radius.

*[procedure]*

`(rect:intersects? a b)`

Returns true if /a/ and /b/ intersect.

*[procedure]*

`(rect:contains? rect other)`

Returns true if /other/ lies completely within /rect/.

*[procedure]*

`(rect:constains-vect? rect v)`

Returns true if /rect/ contains /v/.

*[procedure]*

`(rect:merge a b)`

Returns a bounding box that holds both bounding boxes.

*[procedure]*

`(rect:expand rect v)`

Returns a bounding box that holds both /rect/ and /v/.

*[procedure]*

`(rect:center rect)`

Returns the center of a bounding box.

*[procedure]*

`(rect:area rect)`

Returns the area of the bounding box.

*[procedure]*

`(rect:merged-area a b)`

Merges /a/ and /b/ and returns the area of the merged bounding box.

*[procedure]*

`(rect:segment-query rect a b)`

Returns the fraction along the segment query the bounding box is hit. Returns /infinity/ if it doesn't hit.

*[procedure]*

`(rect:intersects-segment? rect a b)`

Return true if the bounding box intersects the line segment with ends /a/ and /b/.

*[procedure]*

`(rect- rect vect)`

Substracts a vector from a rectangle.

*[procedure]*

`(rect+ rect vect)`

Adds a vector to a rectangle.

#### Lines

*[procedure]*

`(line:create a b)`

Makes a line from two vectors

*[procedure]*

`(line? obj)`

Can also be a rect.

#### Polygons

*[syntax]*

`(polygon:create vects)`

Creates a new polygon from a list of vectors.

*[procedure]*

`(polygon->vects polygon)`

Converts a polygon to a list of vertices.

*[procedure]*

`(polygon:triangulate polygon)`

Triangulates the given polygon and returns an array of vectors.

*[procedure]*

`(polygon:triangulate->triangles polygon)`

Same as polygon-triangulate but returns a list of triangles.

*[procedure]*

`(polygon-convex? polygon)`

Return #t if the given polygon is convex.

*[procedure]*

`(convex-hull vects)`

Returns the convex hull of a group of vertices in clockwise order.

*[procedure]*

`(polygon:convex-hull vects)`

Converts any polygon to a convex polygon.

#### Bezier curves

*[procedure]*

`(bezier:create control-points ...)`

Creates a new bezier curve with a minimum of two control-points.

*[procedure]*

`(bezier:ref bezier n)`

Return the point of a bezier at position n, where 0.0 is the start of the curve and 1.0 is the end.

*[procedure]*

`(bezier->vects bezier accuracy)`

Returns a list of points that make of a bezier curve. A higher /accuracy/ results in a higher resolution (more points).

#### Colours

*[procedure]*

`(rgb:create r g b #!optional (a 1.0)`

Creates a new RGB colour

*[procedure]*

`(rgb:r rgb)`

*[procedure]*

`(rgb:g rgb)`

*[procedure]*

`(rgb:b rgb)`

*[procedure]*

`(rgb:a rgb)`

*[procedure]*

`(rgb->hsv rgb)`

Convers a rgb colour to a hsv colour.

*[procedure]*

`(hsv:create h s v #!optional (a 1.0)`

Creates a new HSV colour

*[procedure]*

`(hsv:h hsv)`

*[procedure]*

`(hsv:s hsv)`

*[procedure]*

`(hsv:v hsv)`

*[procedure]*

`(hsv:a hsv)`

*[procedure]*

`(hsv->rgb hsv)`

Converts a hsv colour to a rgb colour.