module Geode::VectorOperations(N)

Overview

Common operations 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/operations.cr

Instance Method Summary

Instance Method Detail

def &*(scalar : Number) : CommonVector #

Scales each component by the specified amount.

Values will wrap instead of overflowing and raising an error.

Vector[5, -2, 0] &* 3 # => (15, -6, 0)

[View source]
def &+(other : CommonVector(T, N)) : CommonVector forall T #

Adds two vectors together.

Values will wrap instead of overflowing and raising an error.

Vector[5, -2, 0] &+ Vector[2, -1, 4] # => (7, -3, 4)

[View source]
def &-(other : CommonVector(T, N)) : CommonVector forall T #

Subtracts another vector from this one.

Values will wrap instead of overflowing and raising an error.

Vector[5, -2, 0] &- Vector[2, -1, 4] # => (3, -1, -4)

[View source]
def *(scalar : Number) : CommonVector #

Scales each component by the specified amount.

Vector[5, -2, 0] * 3 # => (15, -6, 0)

[View source]
def +(other : CommonVector(T, N)) : CommonVector forall T #

Adds two vectors together.

Vector[5, -2, 0] + Vector[2, -1, 4] # => (7, -3, 4)

[View source]
def -(other : CommonVector(T, N)) : CommonVector forall T #

Subtracts another vector from this one.

Vector[5, -2, 0] - Vector[2, -1, 4] # => (3, -1, -4)

[View source]
def - : self #

Returns a negated vector.

-Vector[5, -2, 0] # => (-5, 2, 0)

[View source]
def /(scalar : Number) : CommonVector #

Scales each component by the specified amount.

Vector[16, -2, 0] / 4 # => (4.0, -0.5, 0.0)

[View source]
def //(scalar : Number) : CommonVector #

Scales each component by the specified amount.

Uses integer division.

Vector[7, -3, 0] // 3 # => (2, -1, 0)

[View source]
def abs : self #

Returns a vector containing the absolute value of each component.

Vector[-5, 42, -20].abs # => (5, 42, 20)

[View source]
def abs2 : self #

Returns a vector containing the square of each component.

Vector[-5, 3, -2].abs2 # => (25, 9, 4)

[View source]
def ceil : self #

Returns a vector with components rounded up to the nearest integer.

Vector[1.2, -5.7, 3.0] # => (2.0, -5.0, 3.0)

[View source]
def clamp(min : CommonVector(T, N), max : CommonVector(T, N)) : CommonVector forall T #

Returns a vector restricted to the minimum and maximum values from other vectors.

min = Vector[-1, -1, -1]
max = Vector[1, 1, 1]
Vector[5, -2, 0].clamp(min, max) # => (1, -1, 0)

[View source]
def clamp(min, max) : CommonVector #

Returns a vector restricted to min and max values.

min and max should be scalar values. Each component of the vector is clipped to these same values.

Vector[5, -2, 0].clamp(-1, 1) # => (1, -1, 0)

[View source]
def clamp(range : Range(CommonVector(T, N), CommonVector(T, N))) : CommonVector forall T #

Returns a vector restricted to the range from other vectors.

min = Vector[-1, -1, -1]
max = Vector[1, 1, 1]
Vector[5, -2, 0].clamp(min..max) # => (1, -1, 0)

[View source]
def clamp(range : Range) : CommonVector #

Returns a vector restricted to a range.

The range should consist of scalar values. Each component of the vector is clipped to the same range.

Vector[5, -2, 0].clamp(-1..1) # => (1, -1, 0)

[View source]
def edge(edge : CommonVector(T, N)) : self forall T #

Returns a new vector where each component is 0 if it's less than the corresponding edge value, or 1 if it's greater.

Vector[1, 2, 3].edge(Vector[3, 2, 1]) # => (0, 1, 1)

[View source]
def edge(edge : T) : self forall T #

Returns a new vector where each component is 0 if it's less than the edge value, or 1 if it's greater.

Vector[1, 2, 3].edge(2) # => (0, 1, 1)

[View source]
def floor : self #

Returns a vector with components rounded down to the nearest integer.

Vector[1.2, -5.7, 3.0] # => (1.0, -6.0, 3.0)

[View source]
def fraction : self #

Returns a vector with the fraction from each component.

This is effectively equal to:

fraction(v) = v - floor(v)
Vector[1.2, -5.7, 3.0] # => (0.2, 0.3, 0.0)

[View source]
def lerp(other : CommonVector(T, N), t : Number) : CommonVector forall T #

Calculates the linear interpolation between two vectors.

t is a value from 0 to 1, where 0 represents this vector and 1 represents other. Any value between 0 and 1 will result in a proportional amount of this vector and other.

This method uses the precise calculation that does not suffer precision loss from high exponential differences.


[View source]
def round(mode : Number::RoundingMode = :ties_even) : self #

Returns a vector with each component rounded.

See Number#round for details.

Vector[1.2, -5.7, 3.0].round # => (1.0, -6.0, 3.0)

[View source]
def round(digits : Number, base = 10, *, mode : Number::RoundingMode = :ties_even) : self #

Returns a vector with each component rounded.

See Number#round for details.

Vector[1.25, -5.77, 3.01].round(1) # => (1.3, -5.8, 3.0)

[View source]
def scale(vector : CommonVector(T, N)) : CommonVector forall T #

Returns a vector with each component scaled by the corresponding component value.

Vector[1, 0, -1].scale(Vector[2, 3, 5]) # => (2, 0, -5)

[View source]
def scale(amount : Number) : CommonVector #

Scales each component by the specified amount.

Vector[5, -2, 0].scale(3) # => (15, -6, 0)

[View source]
def scale!(vector : CommonVector(T, N)) : CommonVector forall T #

Returns a vector with each component scaled by the corresponding component value.

Values will wrap instead of overflowing and raising an error.

Vector[1, 0, -1].scale!(Vector[2, 3, 5]) # => (2, 0, -5)

[View source]
def scale!(amount : Number) : CommonVector #

Scales each component by the specified amount.

Values will wrap instead of overflowing and raising an error.

Vector[5, -2, 0].scale!(3) # => (15, -6, 0)

[View source]
def sign : self #

Returns a vector with components equal to their original sign.

1 is used for positive values, -1 for negative values, and 0 for zero.

Vector[5, 0, -5] # => (1, 0, -1)

[View source]