Vec

Generic struct that represents a vector holding size elements of type T. A vector must contain at least 1 element, and has no upper-bound on the size beyond restrictions imposed by the system.

Constructors

this
this(T[size] elements)

Constructs a vector from an array of elements.

this
this(T[] elements)

Constructs a vector from a variadic list of elements.

this
this(T value)

Constructs a vector where all elements have the given value.

this
this(Vec!(T, size) other)

Constructs a vector as a copy of the other.

Members

Functions

add
Vec!(T, size) add(Vec!(V, size) other)

Adds the given vector to this one.

add
Vec!(T, size) add(V scalar)

Adds the given scalar value to this vector.

copy
Vec!(T, size) copy()

Gets a copy of this vector.

cross
Vec!(T, size) cross(Vec!(T, size) other)

Computes the cross product of this vector and another, and stores the result in this vector.

div
Vec!(T, size) div(V factor)

Divides this vector by a factor, element-wise.

div
Vec!(T, size) div(Vec!(V, size) other)

Divides this vector by another vector, element-wise.

dot
T dot(Vec!(T, size) other)

Determines the dot product of this vector and another vector.

mag
double mag()

Determines the magnitude of this vector.

mag2
double mag2()

Determines the squared magnitude of this vector.

mul
Vec!(T, size) mul(V factor)

Multiplies this vector by a factor, element-wise.

mul
Vec!(T, size) mul(Vec!(V, size) other)

Multiplies this vector by another vector, element-wise.

norm
Vec!(T, size) norm()

Normalizes this vector, such that it will have a magnitude of 1.

opBinary
Vec!(T, size) opBinary(Vec!(V, size) other)

Implements the basic binary operators between two vectors. It supports element-wise addition, subtraction, multiplication, and division.

opBinary
Vec!(T, size) opBinary(V scalar)

Implements the basic binary operators between a vector and a scalar value. It supports element-wise addition, subtraction multiplication, and division.

opBinaryRight
Vec!(T, size) opBinaryRight(V scalar)

Right-hand binary operator implementation. See opBinary above for more info.

opCast
NewVecType opCast()

Implements explicit casting between vector types. You may cast to a type with a different T element type, or a different size, or both. Casting to a different element type will call a normal cast(newType) oldValue for each value. When casting to a smaller vector size, extra elements will be truncated, and when casting to a larger size, missing elements are initialized to zero.

opCmp
int opCmp(Vec!(T, size) other)

Compares this vector to another, based on their magnitudes.

opDispatch
T opDispatch()

Named accessor for common vector fields, which allows you to get the value of specific elements in the vector according to conventional names. - x for the first element. - y for the second element. - z for the third element. - w for the fourth element.

opDispatch
void opDispatch(V value)

Named setter for common vector fields, which allows you to set the value of specific elements in the vector according to conventional names. - x for the first element. - y for the second element. - z for the third element. - w for the fourth element.

opEquals
bool opEquals(Vec!(T, size) other)

Determines if two vectors are equal. Vectors are considered equal when all of their components are equal.

opIndex
T opIndex(size_t i)

Gets the element at a specified index.

opIndexAssign
void opIndexAssign(T value, size_t i)

Inserts an element at the specified index.

opIndexOpAssign
void opIndexOpAssign(V value, size_t i)

Implements op-assignments for indexed values of the vector, so you can do things like v[2] *= 10;. Supports addition, subtraction, multiplication, and division.

opUnary
Vec!(T, size) opUnary()

Implements the basic unary operators on a vector. This supports: - Negation -v: Negates all a vector's components. - Incrementation ++v: Increments all a vector's components by 1. - Decrementation --v: Decrements all a vector's components by 1.

set
Vec!(T, size) set(T[size] elements)

Sets all elements of the vector to those in the specified array.

set
Vec!(T, size) set(T[] elements)

Sets all the elements of the vector to those in the specified variadic array. Note that if the given list of elements is shorter than the vector's size, only the first elements.length elements will be set, and if the list of elements is larger than the vector's size, any extra elements will be ignored.

sub
Vec!(T, size) sub(Vec!(V, size) other)

Subtracts the given vector from this one.

sub
Vec!(T, size) sub(V scalar)

Subtracts the given scalar value from this vector.

toCartesian
Vec!(T, size) toCartesian()

Converts this 2-dimensional vector from Polar to Cartesian coordinates. It is assumed that the first element is the **radius** and the second element is the angle **theta**. The first element becomes the **x** coordinate, and the second becomes the **y** coordinate.

toPolar
Vec!(T, size) toPolar()

Converts this 2-dimensional vector from Cartesian to Polar coordinates. It is assumed that the first element is the **x** coordinate, and the second element is the **y** coordinate. The first element becomes the **radius** and the second becomes the angle **theta**. - The angle is normalized to be within the range [0, 2*PI).

toString
string toString()

Gets a simple string representation of this vector.

Static functions

empty
Vec!(T, size) empty()

Constructs a vector containing all 0's.

sum
Vec!(T, size) sum(Vec!(T, size)[] vectors)

Computes the sum of a given array of vectors.

Static variables

SIZE
size_t SIZE;

A static contstant that can be used to get the size of the vector.

Variables

data
T[size] data;

The internal static array storing the elements of this vector.

Meta