## 2d-primitives

## TOC »

### 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

`(fmod numer denom)`procedureReturns the floating-point remainder of numer/denom (rounded towards zero).

`(clamp f mmin mmax)`procedureClamp a floating-point number to a minimum and a maximum.

`(sqr x)`procedureSquare a number.

`(float-min)`constantMinimum representable floating-point number.

`infinity`constant

#### Constants

`epsilon`constant

`pi`constant

`pi/2`constant

`2pi`constant

`-pi`constant

`360/2pi`constant

`2pi/360`constant

#### Angles

`(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)`procedure

#### Vectors

`(vect:create x y)`procedureReturs a new vector

`(vect? obj)`procedure

`(vect:x v)`procedure

`(vect:y v)`procedure

`(zero-vect)`procedureConstant for the zero vector.

`(vect=? a b #!optional (epsilon .001)`procedureCheck if two vectors are equal.

`(vect+ a b)`procedureAdd two vectors.

`(vect- a #!optional b)`procedureSubtract two vectors or negate a vector.

`(vect* v s)`procedureScalar multiplication.

`(vect:dot a b)`procedureVector dot product.

`(vect:cross a b)`procedure2D 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.

`(vect:perp v)`procedureReturns a perpendicular vector. <procedure>(90 degree rotation)</procedure><br/>

`(vect:vperp v)`procedureReturns a perpendicular vector. <procedure>(-90 degree rotation)</procedure><br/>

`(vect:project a b)`procedureReturns the vector projection of /a/ onto /b/.

`(angle->vect a)`procedureReturns the unit length vector for the given angle (in radians).

`(vect->angle v)`procedureReturns the angular direction v is pointing in (in radians).

`(vect:rotate a b)`procedureUses complex number multiplication to rotate /a/ by /b/. Scaling will occur if /a/ is not a unit vector.

`(vect:unrotate a b)`procedureInverse of vect:rotate

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

`(vect:length v)`procedureReturns the length of v.

`(vect:lerp v1 v2 t)`procedureLinearly interpolate between /a/ and /b/.

`(vect:normalize v)`procedureReturns a normalized copy of v.

`(vect:clamp v len)`procedureClamp v to length len.

`(vect:lerp-const v1 v2 dist)`procedureLinearly interpolate between v1 towards v2 by distance d.

`(vect:dist v1 v2)`procedureReturns the distance between v1 and v2.

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

`(vect:near? a b dist)`procedureReturns true if the distance between v1 and v2 is less than dist.

`(vect:spherical-lerp a b t)`procedureSpherical linearly interpolate between /a/ and /b/.

`(vect:spherical-lerp-const a b angle)`procedureSpherical linearly interpolate between /a/ towards /b/ by no more than angle /angle/ in radians.

#### Bounding Boxes

`(rect:create l r b t)`procedureReturs a new bounding box.

`(rect? obj)`procedureCan also be a line.

`(rect:l rect)`procedure

`(rect:r rect)`procedure

`(rect:b rect)`procedure

`(rect:t rect)`procedure

`(rect:for-circle p r)`procedureConstructs a /rect/ for a circle with the given position and radius.

`(rect:intersects? a b)`procedureReturns true if /a/ and /b/ intersect.

`(rect:contains? rect other)`procedureReturns true if /other/ lies completely within /rect/.

`(rect:constains-vect? rect v)`procedureReturns true if /rect/ contains /v/.

`(rect:merge a b)`procedureReturns a bounding box that holds both bounding boxes.

`(rect:expand rect v)`procedureReturns a bounding box that holds both /rect/ and /v/.

`(rect:center rect)`procedureReturns the center of a bounding box.

`(rect:area rect)`procedureReturns the area of the bounding box.

`(rect:merged-area a b)`procedureMerges /a/ and /b/ and returns the area of the merged bounding box.

`(rect:segment-query rect a b)`procedureReturns the fraction along the segment query the bounding box is hit. Returns /infinity/ if it doesn't hit.

`(rect:intersects-segment? rect a b)`procedureReturn true if the bounding box intersects the line segment with ends /a/ and /b/.

`(rect- rect vect)`procedureSubstracts a vector from a rectangle.

`(rect+ rect vect)`procedureAdds a vector to a rectangle.

#### Lines

`(line:create a b)`procedureMakes a line from two vectors

`(line? obj)`procedureCan also be a rect.

#### Polygons

`(polygon:create vects)`syntaxCreates a new polygon from a list of vectors.

`(polygon->vects polygon)`procedureConverts a polygon to a list of vertices.

`(polygon:triangulate polygon)`procedureTriangulates the given polygon and returns an array of vectors.

`(polygon:triangulate->triangles polygon)`procedureSame as polygon-triangulate but returns a list of triangles.

`(polygon-convex? polygon)`procedureReturn #t if the given polygon is convex.

`(convex-hull vects)`procedureReturns the convex hull of a group of vertices in clockwise order.

`(polygon:convex-hull vects)`procedureConverts any polygon to a convex polygon.

#### Bezier curves

`(bezier:create control-points ...)`procedureCreates a new bezier curve with a minimum of two control-points.

`(bezier:ref bezier n)`procedureReturn the point of a bezier at position n, where 0.0 is the start of the curve and 1.0 is the end.

`(bezier->vects bezier accuracy)`procedureReturns a list of points that make of a bezier curve. A higher /accuracy/ results in a higher resolution (more points).

#### Colours

`(rgb:create r g b #!optional (a 1.0)`procedureCreates a new RGB colour

`(rgb:r rgb)`procedure

`(rgb:g rgb)`procedure

`(rgb:b rgb)`procedure

`(rgb:a rgb)`procedure

`(rgb->hsv rgb)`procedureConvers a rgb colour to a hsv colour.

`(hsv:create h s v #!optional (a 1.0)`procedureCreates a new HSV colour

`(hsv:h hsv)`procedure

`(hsv:s hsv)`procedure

`(hsv:v hsv)`procedure

`(hsv:a hsv)`procedure

`(hsv->rgb hsv)`procedureConverts a hsv colour to a rgb colour.