Next: Rectangles, Previous: Basics, Up: Math [Contents][Index]
Unlike Scheme’s vector data type, which is a sequence of arbitrary
Scheme objects, Chickadee’s (chickadee math vector)
module
provides vectors in the linear algebra sense: Sequences of numbers
specialized for particular coordinate spaces. As of now, Chickadee
provides 2D and 3D vectors, with 4D vector support coming in a future
release.
Here’s a quick example of adding two vectors:
(define v (vec2+ (vec2 1 2) (vec2 3 4)))
Since vectors are used so frequently, the reader macro #v
is
used to cut down on typing:
(define v (vec2+ #v(1 2) #v(3 4)))
A Note About Performance
A lot of time has been spent making Chickadee’s vector operations
perform relatively efficiently in critical code paths where excessive
garbage generation will cause major performance issues. The general
rule is that procedures ending with !
perform an in-place
modification of one of the arguments in order to avoid allocating a
new vector. These procedures are also inlined by Guile’s compiler in
order to take advantage of optimizations relating to floating point
math operations. The downside is that since these are not pure
functions, they do not compose well and create more verbose code.
Return a new 2D vector with coordinates (x, y).
Return a new 2D vector containing the Cartesian representation of the polar coordinate (r, theta). The angle theta is measured in radians.
Return #t
if obj is a 2D vector.
Return the X coordinate of the 2D vector v.
Return the Y coordinate of the 2D vector v.
Return a fresh copy of the 2D vector v.
Return the magnitude of the 2D vector v.
Return the dot product of the 2D vectors v1 and v2.
Return the normalized form of the 2D vector v.
Add x, either a 2D vector or a scalar (i.e. a real number), to the 2D vector v and return a new vector containing the sum.
Subtract x, either a 2D vector or a scalar, from the 2D vector v and return a new vector containing the difference.
Multiply the 2D vector v by x, a 2D vector or a scalar, and return a new vector containing the product.
Set the X coordinate of the 2D vector v to x.
Set the Y coordinate of the 2D vector v to y.
Set the X and Y coordinates of the 2D vector v to x and y, respectively.
Copy the 2D vector source into the 2D vector target.
Perform an in-place modification of the 2D vector v by adding x, a 2D vector or a scalar.
Perform an in-place modification of the 2D vector v by subtracting x, a 2D vector or a scalar.
Perform an in-place modification of the 2D vector v by multiplying it by x, a 2D vector or a scalar.
Return a new 2D vector with coordinates (x, y).
Return #t
if obj is a 3D vector.
Return the X coordinate of the 3D vector v.
Return the Y coordinate of the 3D vector v.
Return the Z coordinate of the 3D vector v.
Return a fresh copy of the 3D vector v.
Return the magnitude of the 3D vector v.
Return the dot product of the 3D vectors v1 and v2.
Return a new 3D vector containing the cross product of v1 and v2.
Return the normalized form of the 3D vector v.
Add x, either a 3D vector or a scalar (i.e. a real number), to the 3D vector v and return a new vector containing the sum.
Subtract x, either a 3D vector or a scalar, from the 3D vector v and return a new vector containing the difference.
Multiply the 3D vector v by x, a 3D vector or a scalar, and return a new vector containing the product.
Set the X coordinate of the 3D vector v to x.
Set the Y coordinate of the 3D vector v to y.
Set the Z coordinate of the 3D vector v to z.
Set the X, Y, and Z coordinates of the 3D vector v to x, y, and z, respectively.
Copy the 3D vector source into the 3D vector target.
Perform an in-place modification of the 3D vector v by adding x, a 3D vector or a scalar.
Perform an in-place modification of the 3D vector v by subtracting x, a 3D vector or a scalar.
Perform an in-place modification of the 3D vector v by multiplying it by x, a 3D vector or a scalar.
Compute the cross product of the 3D vectors v1 and v2 and store the result in dest.
Next: Rectangles, Previous: Basics, Up: Math [Contents][Index]