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


4.2.1 Vectors

(use-modules (sly math vector))

Not to be confused with Guile’s vector data structure, Sly provides dedicated data types for 2D, 3D, and 4D vectors in the mathematical sense. Vector procedures are polymorphic, and will work with any dimensionality that the operation supports. In some cases, such as v+, v-, and v*, scalar values may be passed as well.

The <vector2>, <vector3>, and <vector4> type descriptors have been exposed for use with Guile’s (ice-9 match) pattern matching module.

Record Type: <vector2>

2D vector data type with fields x and y, in that order.

Record Type: <vector3>

3D vector data type with fields x, y, and z, in that order.

Record Type: <vector4>

4D vector data type with fields x, y, z and w, in that order.

Scheme Syntax: vector2 x y

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

Scheme Syntax: vector3 x y z

Create a new 3D vector with coordinates (x, y, z).

Scheme Syntax: vector4 x y z w

Create a new 4D vector with coordinates (x, y, z, w).

Scheme Syntax: vector2? v

Return #t if v is a 2D vector.

Scheme Syntax: vector3? v

Return #t if v is a 3D vector.

Scheme Syntax: vector4? v

Return #t if v is a 4D vector.

Scheme Procedure: vx v

Return the x coordinate of the 2D, 3D, or 4D vector v.

Scheme Procedure: vy v

Return the y coordinate of the 2D, 3D, or 4D vector v.

Scheme Procedure: vz v

Return the z coordinate of the 3D or 4D vector v.

Scheme Procedure: vw v

Return the w coordinate of the 4D vector v.

Scheme Procedure: vmap proc v

Return a new vector that is the result of applying proc to each element of the 2D, 3D, or 4D vector v.

Scheme Procedure: v+ . vectors

Return the sum of all vectors. vectors may contain scalar values, but it may not contain vectors of mixed dimensions.

Scheme Procedure: v- . vectors

Return the difference of all vectors. vectors may contain scalar values, but it may not contain vectors of mixed dimensions.

Scheme Procedure: v* . vectors

Return the product of all vectors. vectors may contain scalar values, but it may not contain vectors of mixed dimensions.

Scheme Procedure: vdot v1 v2

Return the dot product of v1 and v2. Both vectors must be of the same dimensionality.

Scheme Procedure: vcross v1 v2

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

Scheme Procedure: magnitude v

Return the magnitude of the vector v.

Scheme Procedure: normalize v

Return the normalized form of the vector v.

Scheme Procedure: vlerp v1 v2 alpha

Return the linear interpolation of v1 and v2 by the scalar alpha. alpha is expected to be in the range [0, 1].


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