Next: , Previous: , Up: Math   [Contents][Index]


2.2.2 Vectors

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

2.2.2.1 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.

2.2.2.2 2D Vectors

Procedure: vec2 x y

Return a new 2D vector with coordinates (x, y).

Procedure: vec2/polar r theta

Return a new 2D vector containing the Cartesian representation of the polar coordinate (r, theta). The angle theta is measured in radians.

Procedure: vec2? obj

Return #t if obj is a 2D vector.

Procedure: vec2-x v

Return the X coordinate of the 2D vector v.

Procedure: vec2-y v

Return the Y coordinate of the 2D vector v.

Procedure: vec2-copy v

Return a fresh copy of the 2D vector v.

Procedure: vec2-magnitude v

Return the magnitude of the 2D vector v.

Procedure: vec2-dot-product v1 v2

Return the dot product of the 2D vectors v1 and v2.

Procedure: vec2-normalize v

Return the normalized form of the 2D vector v.

Procedure: vec2+ v x

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.

Procedure: vec2- v x

Subtract x, either a 2D vector or a scalar, from the 2D vector v and return a new vector containing the difference.

Procedure: vec2* v x

Multiply the 2D vector v by x, a 2D vector or a scalar, and return a new vector containing the product.

Procedure: set-vec2-x! v x

Set the X coordinate of the 2D vector v to x.

Procedure: set-vec2-y! v y

Set the Y coordinate of the 2D vector v to y.

Procedure: set-vec2! v x y

Set the X and Y coordinates of the 2D vector v to x and y, respectively.

Procedure: vec2-copy! source target

Copy the 2D vector source into the 2D vector target.

Procedure: vec2-add! v x

Perform an in-place modification of the 2D vector v by adding x, a 2D vector or a scalar.

Procedure: vec2-sub! v x

Perform an in-place modification of the 2D vector v by subtracting x, a 2D vector or a scalar.

Procedure: vec2-mult! v x

Perform an in-place modification of the 2D vector v by multiplying it by x, a 2D vector or a scalar.

2.2.2.3 3D Vectors

Procedure: vec3 x y

Return a new 2D vector with coordinates (x, y).

Procedure: vec3? obj

Return #t if obj is a 3D vector.

Procedure: vec3-x v

Return the X coordinate of the 3D vector v.

Procedure: vec3-y v

Return the Y coordinate of the 3D vector v.

Procedure: vec3-z v

Return the Z coordinate of the 3D vector v.

Procedure: vec3-copy v

Return a fresh copy of the 3D vector v.

Procedure: vec3-magnitude v

Return the magnitude of the 3D vector v.

Procedure: vec3-dot-product v1 v2

Return the dot product of the 3D vectors v1 and v2.

Procedure: vec3-normalize v

Return the normalized form of the 3D vector v.

Procedure: vec3+ v x

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.

Procedure: vec3- v x

Subtract x, either a 3D vector or a scalar, from the 3D vector v and return a new vector containing the difference.

Procedure: vec3* v x

Multiply the 3D vector v by x, a 3D vector or a scalar, and return a new vector containing the product.

Procedure: set-vec3-x! v x

Set the X coordinate of the 3D vector v to x.

Procedure: set-vec3-y! v y

Set the Y coordinate of the 3D vector v to y.

Procedure: set-vec3-z! v z

Set the Z coordinate of the 3D vector v to z.

Procedure: set-vec3! v x y z

Set the X, Y, and Z coordinates of the 3D vector v to x, y, and z, respectively.

Procedure: vec3-copy! source target

Copy the 3D vector source into the 3D vector target.

Procedure: vec3-add! v x

Perform an in-place modification of the 3D vector v by adding x, a 3D vector or a scalar.

Procedure: vec3-sub! v x

Perform an in-place modification of the 3D vector v by subtracting x, a 3D vector or a scalar.

Procedure: vec3-mult! v x

Perform an in-place modification of the 3D vector v by multiplying it by x, a 3D vector or a scalar.


Next: , Previous: , Up: Math   [Contents][Index]