module Geode::VectorGeometry(N)

Overview

Geometric methods for vectors.

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

Direct including types

Defined in:

geode/vectors/geometry.cr

Instance Method Summary

Instance Method Detail

def 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

[View source]
def 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

[View source]
def 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

[View source]
def 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.


[View source]
def length #

Computes the magnitude (length) of this vector.

Same as #mag.

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

[View source]
def mag #

Computes the magnitude (length) of this vector.

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

[View source]
def 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

[View source]
def normalize : CommonVector #

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

[View source]
def 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)

[View source]
def 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)

[View source]
def 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)

[View source]
def 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

[View source]