gfm



struct  Vector(T, int N);

Generic 1D small vector.

Parameters
N number of elements
T type of elements

pure nothrow @nogc ref Vector  opAssign(U)(U x) if (isAssignable!(T, U));

Assign a Vector from a compatible type.


pure nothrow @nogc ref Vector  opAssign(U)(U arr) if (isStaticArray!U && isAssignable!(T, typeof(arr[0])) && arr.length == N);

Assign a Vector with a static array type.


pure nothrow @nogc ref Vector  opAssign(U)(U arr) if (isDynamicArray!U && isAssignable!(T, typeof(arr[0])));

Assign with a dynamic array.

Size is checked in debug-mode.


pure nothrow @nogc ref Vector  opAssign(U)(U u) if (is(U : Vector));

Assign from a samey Vector.


pure nothrow @nogc ref Vector  opAssign(U)(U x) if (isVector!U && isAssignable!(T, U._T) && !is(U : Vector) && U._N == _N);

Assign from other vectors types (same size, compatible type).


inout pure nothrow @nogc @property inout(T)*  ptr();

Returns
a pointer to content.

const nothrow string  toString();

Converts to a pretty string.


const pure nothrow @nogc @property auto  opDispatch(string op, U = void)() if (isValidSwizzle!op);

Implements swizzling.

Example:
vec4i vi = [4, 1, 83, 10];

assert(vi.zxxyw == [83, 4, 4, 1, 10]);


pure @nogc @property void  opDispatch(string op, U)(U x) if (op.length >= 2 && isValidSwizzleUnique!op && is(typeof(Vector!(T, op.length)(x))));

Support swizzling assignment like in shader languages.

Example:
vec3f v = [0, 1, 2];

v.yz = v.zx;

assert(v == [0, 2, 0]);


const pure nothrow @nogc U  opCast(U)() if (isVector!U && U._N == _N);

Casting to small vectors of the same size.

Example:
vec4f vf;

vec4d vd = cast!(vec4d)vf;


const pure nothrow @nogc int  opDollar();

Implement slices operator overloading.

Allows to go back to slice world.

Returns
length.

pure nothrow @nogc T[]  opSlice();

Returns
a slice which covers the whole Vector.

const pure nothrow @nogc T  squaredLength();

Returns
squared length.

const pure nothrow @nogc T  length();

Returns
Euclidean  length

const pure nothrow @nogc T  inverseLength();

Returns
Inverse of Euclidean length.

const pure nothrow @nogc T  fastInverseLength();

Faster but less accurate inverse of Euclidean length.

Returns
Inverse of Euclidean length.

const pure nothrow @nogc T  distanceTo(Vector other);

Returns
Euclidean distance between this and other.

pure nothrow @nogc void  normalize();

In-place normalization.


const pure nothrow @nogc Vector  normalized();

Returns
Normalized vector.

pure nothrow @nogc void  fastNormalize();

Faster but less accurate in-place normalization.


const pure nothrow @nogc Vector  fastNormalized();

Faster but less accurate vector normalization.

Returns
Normalized vector.

const pure nothrow @nogc Vector  getOrthogonalVector();

Gets an orthogonal vector from a 3-dimensional vector.

Doesn’t normalize the output.

Authors
Sam Hocevar
See Also
Source at lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts.

enum auto  isVector(T);

True if

T
is some kind of
Vector


template  DimensionType(T : Vector!U, U...)

Get the numeric type used to measure a vectors's coordinates.

Examples
static assert(is(DimensionType!vec2f == float));
static assert(is(DimensionType!vec3d == double));

pure nothrow @nogc Vector!(T, N)  minByElem(T, int N)(const Vector!(T, N) a, const Vector!(T, N) b);

Element-wise minimum.


pure nothrow @nogc Vector!(T, N)  maxByElem(T, int N)(const Vector!(T, N) a, const Vector!(T, N) b);

Element-wise maximum.


pure nothrow @nogc T  dot(T, int N)(const Vector!(T, N) a, const Vector!(T, N) b);

Returns
Dot product.

pure nothrow @nogc Vector!(T, 3)  cross(T)(const Vector!(T, 3) a, const Vector!(T, 3) b);

Returns
3D  cross product.

Thanks to vuaru for corrections.

pure nothrow @nogc Vector!(T, N)  reflect(T, int N)(const Vector!(T, N) a, const Vector!(T, N) b);

3D  reflect, like the GLSL function.

Returns
a reflected by normal b.
Examples
// reflect a 2D vector across the x axis (the normal points along the y axis)
assert(vec2f(1,1).reflect(vec2f(0,1)) == vec2f(1,-1));
assert(vec2f(1,1).reflect(vec2f(0,-1)) == vec2f(1,-1));

// note that the normal must be, well, normalized:
assert(vec2f(1,1).reflect(vec2f(0,20)) != vec2f(1,-1));

// think of this like a ball hitting a flat floor at an angle.
// the x and y components remain unchanged, and the z inverts
assert(vec3f(2,3,-0.5).reflect(vec3f(0,0,1)) == vec3f(2,3,0.5));

pure nothrow @nogc T  angleBetween(T, int N)(const Vector!(T, N) a, const Vector!(T, N) b);

Returns
angle between vectors.
See Also
"The Right Way to Calculate Stuff" at www.plunk.org/~hatch/rightway.php