struct

Geode::Vector(T, N)

Inherits Geode::CommonVector < Geode::VectorOperations < Geode::VectorMatrices < Geode::VectorGeometry < Geode::VectorComparison < Indexable < Enumerable < Iterable < Struct < Value < Object

Generic vector type. Provides a collection of scalars of the same type.

T is the scalar type. N is a positive integer indicating the number of components.

Constructors

new(vector : CommonVector(T, N))

Copies the contents of another vector.

Source
new(array : StaticArray(T, N))

Constructs the vector with pre-existing values.

The memory allocated for components must match the size of the vector.

Source
new(components : Indexable(T))

Creates a new matrix from a collection of components.

The size of components must be equal to N.

Vector(Int32, 5).new([1, 2, 3, 4, 5]) # => (1, 2, 3, 4, 5)
Source
new

Constructs the vector by yielding for each component.

The value of each component should be returned from the block. The block will be given the index of each component as an argument.

Vector(Int32, 3).new { |i| i * 5 } # => (0, 5, 10)
Source
zero

Constructs a zero-vector.

Each component will have a scalar value equal to the type's'zero value. This is done by calling T.zero for each component. The type T must have a class method

Vector(Float32, 3).zero # => (0.0, 0.0, 0.0)
Source

Class methods

[](*elements)

Constructs a vector with existing components.

The type of the components is specified by the type parameter. Each value is cast to the type T.

Vector(Float32, 5)[1, 2, 3, 4, 5] # => (1.0, 2.0, 3.0, 4.0, 5.0)
Source

Instance methods

cross(other : CommonVector(U, 3)) : CommonVector forall U

Computes the cross-product of this and another vector.

Raises a compilation error if the vector size (N) isn't 3.

Vector[1, 3, 4].cross(Vector[2, -5, 8]) # => (44, 0, -11)
Source
map

Returns a new vector where components are mapped by the given block.

vector = Vector[1, 2, 3]
vector.map { |v| v * 2 } # => (2, 4, 6)
Source
to_slice

Returns a slice that points to the components in this vector.

Source
to_unsafe

Returns a pointer to the data for this vector.

The components are tightly packed and ordered consecutively in memory.

Source
unsafe_fetch(index : Int)

Retrieves the scalar value of the component at the given index, without checking size boundaries.

End-users should never invoke this method directly. Instead, methods like #[] and #[]? should be used.

This method should only be directly invoked if the index is certain to be in bounds.

Source
w

Retrieves the w component.

Raises a compilation error if the vector size (N) is less than 4.

Source
x

Retrieves the x component.

Raises a compilation error if the vector size (N) is less than 1.

Source
y

Retrieves the y component.

Raises a compilation error if the vector size (N) is less than 2.

Source
z

Retrieves the z component.

Raises a compilation error if the vector size (N) is less than 3.

Source

Macros

[](*components)

Constructs the vector with existing components.

The type of the components is derived from the type of each argument. The size of the vector is determined by the number of components.

Vector[1, 2, 3] # => (1, 2, 3)
Source