Vector

Vector representation

struct Vector (
T
int size
) {}

Constructors

this
this(Vector!(T2, size2) v)

Vector constructor. Supports initializing from vector of arbitrary length and type

this
this(A components)

Array constructor

this
this(T2[arrSize] components)

Static array constructor

this
this(F components)

Tuple constructor

this
this(S str)

String constructor

Members

Functions

clamp
void clamp(T minv, T maxv)

Clamp components to min/max value

normalize
void normalize()

Set vector length to 1

opAssign
void opAssign(Vector!(T2, size2) v)

Vector!(T,size) = Vector!(T,size2)

opAssign
void opAssign(T2 ev)

Vector!(T,size) = Vector!(T,size2) for enums

opBinary
Vector!(T, size) opBinary(Vector!(T, size) v)

Vector!(T,size) + Vector!(T,size)

opBinary
Vector!(T, size) opBinary(Vector!(T, size) v)

Vector!(T,size) - Vector!(T,size)

opBinary
Vector!(T, size) opBinary(Vector!(T, size) v)

Vector!(T,size) * Vector!(T,size)

opBinary
Vector!(T, size) opBinary(Vector!(T, size) v)

Vector!(T,size) / Vector!(T,size)

opBinary
Vector!(T, size) opBinary(T t)

Vector!(T,size) + T

opBinary
Vector!(T, size) opBinary(T t)

Vector!(T,size) - T

opBinary
Vector!(T, size) opBinary(T t)

Vector!(T,size) * T

opBinary
Vector!(T, size) opBinary(T t)

Vector!(T,size) / T

opBinary
Vector!(T, size) opBinary(T2 t)

Vector!(T,size) % T

opBinaryRight
Vector!(T, size) opBinaryRight(T t)

T * Vector!(T,size)

opIndex
T opIndex(size_t index)

T = Vector!(T,size)index

opIndexAssign
void opIndexAssign(T n, size_t index)

Vector!(T,size)index = T

opOpAssign
Vector!(T, size) opOpAssign(Vector!(T, size) v)

Vector!(T,size) += Vector!(T,size)

opOpAssign
Vector!(T, size) opOpAssign(Vector!(T, size) v)

Vector!(T,size) -= Vector!(T,size)

opOpAssign
Vector!(T, size) opOpAssign(Vector!(T, size) v)

Vector!(T,size) *= Vector!(T,size)

opOpAssign
Vector!(T, size) opOpAssign(Vector!(T, size) v)

Vector!(T,size) /= Vector!(T,size)

opOpAssign
Vector!(T, size) opOpAssign(T t)

Vector!(T,size) += T

opOpAssign
Vector!(T, size) opOpAssign(T t)

Vector!(T,size) -= T

opOpAssign
Vector!(T, size) opOpAssign(T t)

Vector!(T,size) *= T

opOpAssign
Vector!(T, size) opOpAssign(T t)

Vector!(T,size) /= T

opOpAssign
Vector!(T, size) opOpAssign(T2 t)

Vector!(T,size) %= T

opSlice
auto opSlice(size_t index1, size_t index2)

T[] = Vector!(T,size)index1..index2

opSlice
auto opSlice()

T = Vector!(T,size)[]

opSliceAssign
T opSliceAssign(T t, size_t index1, size_t index2)

Vector!(T,size)index1..index2 = T

opSliceAssign
T opSliceAssign(T t)

Vector!(T,size)[] = T

opUnary
Vector!(T, size) opUnary()

-Vector!(T,size)

opUnary
Vector!(T, size) opUnary()

+Vector!(T,size)

Properties

isZero
bool isZero [@property getter]

Return true if all components are zero

length
T length [@property getter]

Get vector length

lengthsqr
T lengthsqr [@property getter]

Get vector length squared

normalized
Vector!(T, size) normalized [@property getter]

Return normalized copy

toString
string toString [@property getter]

Convert to string

Templates

opDispatch
template opDispatch(string s)

Swizzling

Unions

__anonymous
union __anonymous

Vector components

Examples

1 {
2     const vec3 a = vec3(10.5f, 20.0f, 33.12345f);
3     const vec3 b = -a;
4     const vec3 c = +a - b;
5     const vec3 d = a * b / c;
6 
7     assert(isAlmostZero(to!vec3(c.toString()) - c));
8 
9     const vec3 v = vec3(10, 10, 10);
10     const vec3 vRes = (v / 10) * 2 - 1 + 5;
11     assert(isAlmostZero(vRes - vec3(6, 6, 6)));
12     assert(!vRes.isZero);
13 
14     ivec2 ab = ivec2(5, 15);
15     ab += ivec2(20, 30);
16     ab *= 3;
17 
18     assert(ab[0] == 75 && ab[1] == 135);
19 
20     auto len = c.length();
21     auto lensqr = c.lengthsqr();
22     auto dist = distance(a, b);
23 
24     auto xy = a[0..1];
25     auto n = a[];
26 
27     vec3 v1 = vec3(2.0f, 0.0f, 1.0f);
28     ivec3 v2 = v1;
29     assert(ivec3(v1) == ivec3(2, 0, 1));
30 
31     vec3 v3 = [0, 2, 3.5];
32     assert(v3 == vec3(0.0f, 2.0f, 3.5f));
33 
34     ivec3 v4 = [7, 8, 3];
35     v4 %= 2;
36     assert(v4 == ivec3(1, 0, 1));
37 }
38 
39 {
40     Vector3f a = Vector3f(1, 2, 3);
41     Vector2f b = Vector2f(a);
42     assert(b == Vector2f(1, 2));
43 }
44 
45 {
46     Vector3f a = Vector3f([0, 1]);
47     assert(isNaN(a.z));
48 }
49 
50 {
51     Vector3f a = Vector3f(0, 1, 2);
52     a += 1;
53     assert(a == Vector3f(1, 2, 3));
54     a *= 2;
55     assert(a == Vector3f(2, 4, 6));
56     a -= 1;
57     assert(a == Vector3f(1, 3, 5));
58     a /= 3;
59     assert(a.y == 1);
60 }
61 
62 {
63     Vector3f a;
64     a[1] = 3;
65     assert(a.y == 3);
66 
67     a[0..3] = 1;
68     assert(a == Vector3f(1, 1, 1));
69 
70     a[] = 0;
71     assert(a == Vector3f(0, 0, 0));
72 }
73 
74 {
75     Vector3i a = Vector3i(0, 0, 3);
76     a = a.normalized;
77     assert(a == Vector3i(0, 0, 1));
78     assert(a.length == 1);
79 }
80 
81 {
82     Vector3f a = Vector3f(0, 0, 0);
83     assert(a.isZero);
84 }
85 
86 {
87     Vector3f a = Vector3f(2, -3, 0);
88     a.clamp(-1, 1);
89     assert(a == Vector3f(1, -1, 0));
90 }
91 
92 {
93     Vector3f a = Vector3f(2, 5, 7);
94     Vector4f b = Vector4f(a);
95     assert(b == Vector4f(2, 5, 7, float.nan));
96 }
97 
98 {
99     Vector3f a = Vector3f([0, 1]);
100     assert(a == Vector3f(0, 1, float.nan));
101 
102     Vector4f b = a.xyy;
103     assert(b == Vector4f(0, 1, 1, float.nan));
104 }
105 
106 {
107     Vector3f a = Vector3f(1, 2, 3);
108     a = a + 1;
109     assert(a == Vector3f(2, 3, 4));
110     a = a * 2;
111     assert(a == Vector3f(4, 6, 8));
112     a = a - 2;
113     assert(a == Vector3f(2, 4, 6));
114     a = a / 2;
115     assert(a == Vector3f(1, 2, 3));
116 
117     Vector3f b = Vector3f(3, 2, 1);
118     b += a;
119     assert(b == Vector3f(4, 4, 4));
120     b *= b;
121     assert(b == Vector3f(16, 16, 16));
122     b /= Vector3f(8, 4, 2);
123     assert(b == Vector3f(2, 4, 8));
124     b -= a;
125     assert(b == Vector3f(1, 2, 5));
126 }
127 
128 {
129     Vector3f v = Vector3f(0, 0, 0);
130     v[0] = 5;
131     v[1] = 2;
132     assert(v == Vector3f(5, 2, 0));
133     v[1..3] = 12;
134     assert(v == Vector3f(5, 12, 12));
135     v[] = 0;
136     assert(v == Vector3f(0, 0, 0));
137 }
138 
139 {
140     Vector4f a = Vector4f(2, 4, 6, 8);
141     Vector4f b = a.wxyz;
142     assert(b == Vector4f(8, 2, 4, 6));
143     float d = dot(a, b);
144     assert(d == 96.0f);
145 }
146 
147 {
148     Vector2f a = Vector2f(1, 2);
149     Vector2f b = Vector2f(2, 1);
150     float d = dot(a, b);
151     assert(d == 4.0f);
152 }

Meta