chickadee » 2d-primitives

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

(fmod numer denom) procedure

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

(clamp f mmin mmax) procedure

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

(sqr x) procedure

Square a number.

(float-min) constant

Minimum 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) procedure

Returs a new vector

(vect? obj) procedure
(vect:x v) procedure
(vect:y v) procedure
(zero-vect) procedure

Constant for the zero vector.

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

Check if two vectors are equal.

(vect+ a b) procedure

Add two vectors.

(vect- a #!optional b) procedure

Subtract two vectors or negate a vector.

(vect* v s) procedure

Scalar multiplication.

(vect:dot a b) procedure

Vector dot product.

(vect:cross a b) procedure

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.

(vect:perp v) procedure

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

(vect:vperp v) procedure

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

(vect:project a b) procedure

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

(angle->vect a) procedure

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

(vect->angle v) procedure

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

(vect:rotate a b) procedure

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

(vect:unrotate a b) procedure

Inverse of vect:rotate

(vect:length-squared v) procedure

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

(vect:length v) procedure

Returns the length of v.

(vect:lerp v1 v2 t) procedure

Linearly interpolate between /a/ and /b/.

(vect:normalize v) procedure

Returns a normalized copy of v.

(vect:clamp v len) procedure

Clamp v to length len.

(vect:lerp-const v1 v2 dist) procedure

Linearly interpolate between v1 towards v2 by distance d.

(vect:dist v1 v2) procedure

Returns the distance between v1 and v2.

(vect:dist-squared v1 v2) procedure

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

(vect:near? a b dist) procedure

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

(vect:spherical-lerp a b t) procedure

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

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

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

Bounding Boxes

(rect:create l r b t) procedure

Returs a new bounding box.

(rect? obj) procedure

Can 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) procedure

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

(rect:intersects? a b) procedure

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

(rect:contains? rect other) procedure

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

(rect:constains-vect? rect v) procedure

Returns true if /rect/ contains /v/.

(rect:merge a b) procedure

Returns a bounding box that holds both bounding boxes.

(rect:expand rect v) procedure

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

(rect:center rect) procedure

Returns the center of a bounding box.

(rect:area rect) procedure

Returns the area of the bounding box.

(rect:merged-area a b) procedure

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

(rect:segment-query rect a b) procedure

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

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

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

(rect- rect vect) procedure

Substracts a vector from a rectangle.

(rect+ rect vect) procedure

Adds a vector to a rectangle.

Lines

(line:create a b) procedure

Makes a line from two vectors

(line? obj) procedure

Can also be a rect.

Polygons

(polygon:create vects) syntax

Creates a new polygon from a list of vectors.

(polygon->vects polygon) procedure

Converts a polygon to a list of vertices.

(polygon:triangulate polygon) procedure

Triangulates the given polygon and returns an array of vectors.

(polygon:triangulate->triangles polygon) procedure

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

(polygon-convex? polygon) procedure

Return #t if the given polygon is convex.

(convex-hull vects) procedure

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

(polygon:convex-hull vects) procedure

Converts any polygon to a convex polygon.

Bezier curves

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

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

(bezier:ref bezier n) procedure

Return 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) procedure

Returns 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) procedure

Creates a new RGB colour

(rgb:r rgb) procedure
(rgb:g rgb) procedure
(rgb:b rgb) procedure
(rgb:a rgb) procedure
(rgb->hsv rgb) procedure

Convers a rgb colour to a hsv colour.

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

Creates a new HSV colour

(hsv:h hsv) procedure
(hsv:s hsv) procedure
(hsv:v hsv) procedure
(hsv:a hsv) procedure
(hsv->rgb hsv) procedure

Converts a hsv colour to a rgb colour.

Contents »