module

Geode::VectorGeometry(N)

Geometric methods for vectors.

Intended to be used as a mix-in on vector types. N is the number of components in the vector.

Instance methods

angle(other : CommonVector(T, N)) : Number forall T

Computes the angle between this vector and another.

Returns the value as radians. The value will be between 0 and pi.

The smallest angle between the vectors is calculated.

Vector[1, 1].angle(Vector[-1, 0]) # => 2.35619449
Vector[1, 1].angle(Vector[1, -1]) # => 1.570796327
Source
dot(other : CommonVector(T, N)) forall T

Computes the dot-product of this vector and another.

Vector[2, 5, 7].dot(Vector[1, 0, -5]) # => -33
Source
dot!(other : CommonVector(T, N)) forall T

Computes the dot-product of this vector and another.

Values will wrap instead of overflowing and raising an error.

Vector[2, 5, 7].dot!(Vector[1, 0, -5]) # => -33
Source
forward(surface : CommonVector(T, N)) : CommonVector forall T

Orients a vector to point in the same direction as another.

The surface is a vector to orient with.

Source
length

Computes the magnitude (length) of this vector.

Same as #mag.

Vector[3, 4].length # => 5
Source
mag

Computes the magnitude (length) of this vector.

Vector[3, 4].mag # => 5
Source
mag2

Computes the magnitude (length) squared of this vector.

This method does not perform a square-root operation, making it faster than #mag if the square-root isn't needed.

Vector[3, 4].mag2 # => 25
Source
normalize

Returns a unit vector.

Scales the components of the vector equally so that the magnitude (length) is one.

Vector[1, 0, -1].normalize.mag # => 1.0
Source
project(other : CommonVector(T, N)) : CommonVector forall T

Computes the projection of this vector onto another vector.

Vector[3, 4].project(Vector[1, 0]) # => (3, 0)
Source
reflect(surface : CommonVector(T, N)) : CommonVector forall T

Computes a reflected vector on a surface.

The surface is a normal vector for the surface to reflect against.

Vector[-1, 1].reflect(Vector[0, 1]) # => (1, 1)
Source
refract(surface : CommonVector(T, N), eta : Number) : CommonVector forall T

Computes a refracted vector through a surface.

The surface is a normal vector for the surface to travel through. eta is the ratio of refractive indices. Returns a zero vector for total internal reflection.

Vector[-1, 1].refract(Vector[0, 1], 1.333) # => (-1.333, -1.0)
Source
scale_to(length : Number) : CommonVector

Returns a vector with the magnitude specified.

Scales the components of the vector equally.

Vector[1, 0, -1].scale_to(2).mag # => 2.0
Source