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 denomprocedure

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

clamp f mmin mmaxprocedure

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

sqr xprocedure

Square a number.

float-minconstant

Minimum representable floating-point number.

infinityconstant

Constants

epsilonconstant
piconstant
pi/2constant
2piconstant
-piconstant
360/2piconstant
2pi/360constant

Angles

radian->degree radiansprocedure
degree->radian degreesprocedure
flip-radian-h radiansprocedure
flip-radian-v radiansprocedure
flip-degree-h degreesprocedure
flip-degree-v degreesprocedure

Vectors

vect:create x yprocedure

Returs a new vector

vect? objprocedure
vect:x vprocedure
vect:y vprocedure
zero-vectprocedure

Constant for the zero vector.

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

Check if two vectors are equal.

vect+ a bprocedure

Add two vectors.

vect- a #!optional bprocedure

Subtract two vectors or negate a vector.

vect* v sprocedure

Scalar multiplication.

vect:dot a bprocedure

Vector dot product.

vect:cross a bprocedure

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 vprocedure

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

vect:vperp vprocedure

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

vect:project a bprocedure

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

angle->vect aprocedure

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

vect->angle vprocedure

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

vect:rotate a bprocedure

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

vect:unrotate a bprocedure

Inverse of vect:rotate

vect:length-squared vprocedure

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

vect:length vprocedure

Returns the length of v.

vect:lerp v1 v2 tprocedure

Linearly interpolate between /a/ and /b/.

vect:normalize vprocedure

Returns a normalized copy of v.

vect:clamp v lenprocedure

Clamp v to length len.

vect:lerp-const v1 v2 distprocedure

Linearly interpolate between v1 towards v2 by distance d.

vect:dist v1 v2procedure

Returns the distance between v1 and v2.

vect:dist-squared v1 v2procedure

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

vect:near? a b distprocedure

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

vect:spherical-lerp a b tprocedure

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

vect:spherical-lerp-const a b angleprocedure

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

Bounding Boxes

rect:create l r b tprocedure

Returs a new bounding box.

rect? objprocedure

Can also be a line.

rect:l rectprocedure
rect:r rectprocedure
rect:b rectprocedure
rect:t rectprocedure
rect:for-circle p rprocedure

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

rect:intersects? a bprocedure

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

rect:contains? rect otherprocedure

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

rect:constains-vect? rect vprocedure

Returns true if /rect/ contains /v/.

rect:merge a bprocedure

Returns a bounding box that holds both bounding boxes.

rect:expand rect vprocedure

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

rect:center rectprocedure

Returns the center of a bounding box.

rect:area rectprocedure

Returns the area of the bounding box.

rect:merged-area a bprocedure

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

rect:segment-query rect a bprocedure

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

rect:intersects-segment? rect a bprocedure

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

rect- rect vectprocedure

Substracts a vector from a rectangle.

rect+ rect vectprocedure

Adds a vector to a rectangle.

Lines

line:create a bprocedure

Makes a line from two vectors

line? objprocedure

Can also be a rect.

Polygons

(polygon:create vects)syntax

Creates a new polygon from a list of vectors.

polygon->vects polygonprocedure

Converts a polygon to a list of vertices.

polygon:triangulate polygonprocedure

Triangulates the given polygon and returns an array of vectors.

polygon:triangulate->triangles polygonprocedure

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

polygon-convex? polygonprocedure

Return #t if the given polygon is convex.

convex-hull vectsprocedure

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

polygon:convex-hull vectsprocedure

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 nprocedure

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 accuracyprocedure

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 rgbprocedure
rgb:g rgbprocedure
rgb:b rgbprocedure
rgb:a rgbprocedure
rgb->hsv rgbprocedure

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 hsvprocedure
hsv:s hsvprocedure
hsv:v hsvprocedure
hsv:a hsvprocedure
hsv->rgb hsvprocedure

Converts a hsv colour to a rgb colour.

Contents »