Struct Vector

Vector representation

struct Vector(T, int size) ;

Constructors

NameDescription
this (v) Vector constructor. Supports initializing from vector of arbitrary length and type
this (components) Array constructor
this (components) Static array constructor
this (components) Tuple constructor
this (str) String constructor

Fields

NameTypeDescription
arrayof T[size]Vector components

Properties

NameTypeDescription
elements[set] stringSymbolic element access
isZero[get] boolReturn true if all components are zero
length[get] TGet vector length
lengthsqr[get] TGet vector length squared
normalized[get] Vector!(T,size)Return normalized copy
toString[get] stringConvert to string

Methods

NameDescription
clamp (minv, maxv) Clamp components to min/max value
normalize () Set vector length to 1
opAssign (v) Vector!(T,size) = Vector!(T,size2)
opAssign (ev) Vector!(T,size) = Vector!(T,size2) for enums
opBinary (v) Vector!(T,size) + Vector!(T,size)
opBinary (v) Vector!(T,size) - Vector!(T,size)
opBinary (v) Vector!(T,size) * Vector!(T,size)
opBinary (v) Vector!(T,size) / Vector!(T,size)
opBinary (t) Vector!(T,size) + T
opBinary (t) Vector!(T,size) - T
opBinary (t) Vector!(T,size) * T
opBinary (t) Vector!(T,size) / T
opBinary (t) Vector!(T,size) % T
opBinaryRight (t) T * Vector!(T,size)
opIndex (index) T = Vector!(T,size)[index]
opIndexAssign (n, index) Vector!(T,size)[index] = T
opOpAssign (v) Vector!(T,size) += Vector!(T,size)
opOpAssign (v) Vector!(T,size) -= Vector!(T,size)
opOpAssign (v) Vector!(T,size) *= Vector!(T,size)
opOpAssign (v) Vector!(T,size) /= Vector!(T,size)
opOpAssign (t) Vector!(T,size) += T
opOpAssign (t) Vector!(T,size) -= T
opOpAssign (t) Vector!(T,size) *= T
opOpAssign (t) Vector!(T,size) /= T
opOpAssign (t) Vector!(T,size) %= T
opSlice (index1, index2) T[] = Vector!(T,size)[index1..index2]
opSlice () T = Vector!(T,size)[]
opSliceAssign (t, index1, index2) Vector!(T,size)[index1..index2] = T
opSliceAssign (t) Vector!(T,size)[] = T
opUnary () -Vector!(T,size)
opUnary () +Vector!(T,size)

Templates

NameDescription
opDispatch Swizzling

Example

{
    const vec3 a = vec3(10.5f, 20.0f, 33.12345f);
    const vec3 b = -a;
    const vec3 c = +a - b;
    const vec3 d = a * b / c;

    assert(isAlmostZero(to!vec3(c.toString()) - c));

    const vec3 v = vec3(10, 10, 10);
    const vec3 vRes = (v / 10) * 2 - 1 + 5;
    assert(isAlmostZero(vRes - vec3(6, 6, 6)));
    assert(!vRes.isZero);

    ivec2 ab = ivec2(5, 15);
    ab += ivec2(20, 30);
    ab *= 3;

    assert(ab[0] == 75 && ab[1] == 135);

    auto len = c.length();
    auto lensqr = c.lengthsqr();
    auto dist = distance(a, b);

    auto xy = a[0..1];
    auto n = a[];

    vec3 v1 = vec3(2.0f, 0.0f, 1.0f);
    ivec3 v2 = v1;
    assert(ivec3(v1) == ivec3(2, 0, 1));

    vec3 v3 = [0, 2, 3.5];
    assert(v3 == vec3(0.0f, 2.0f, 3.5f));

    ivec3 v4 = [7, 8, 3];
    v4 %= 2;
    assert(v4 == ivec3(1, 0, 1));
}

{
    Vector3f a = Vector3f(1, 2, 3);
    Vector2f b = Vector2f(a);
    assert(b == Vector2f(1, 2));
}

{
    Vector3f a = Vector3f([0, 1]);
    assert(isNaN(a.z));
}

{
    Vector3f a = Vector3f(0, 1, 2);
    a += 1;
    assert(a == Vector3f(1, 2, 3));
    a *= 2;
    assert(a == Vector3f(2, 4, 6));
    a -= 1;
    assert(a == Vector3f(1, 3, 5));
    a /= 3;
    assert(a.y == 1);
}

{
    Vector3f a;
    a[1] = 3;
    assert(a.y == 3);

    a[0..3] = 1;
    assert(a == Vector3f(1, 1, 1));

    a[] = 0;
    assert(a == Vector3f(0, 0, 0));
}

{
    Vector3i a = Vector3i(0, 0, 3);
    a = a.normalized;
    assert(a == Vector3i(0, 0, 1));
    assert(a.length == 1);
}

{
    Vector3f a = Vector3f(0, 0, 0);
    assert(a.isZero);
}

{
    Vector3f a = Vector3f(2, -3, 0);
    a.clamp(-1, 1);
    assert(a == Vector3f(1, -1, 0));
}