Vector constructor. Supports initializing from vector of arbitrary length and type
Array constructor
Static array constructor
Tuple constructor
String constructor
Clamp components to min/max value
Set vector length to 1
Vector!(T,size) = Vector!(T,size2)
Vector!(T,size) = Vector!(T,size2) for enums
Vector!(T,size) + Vector!(T,size)
Vector!(T,size) - Vector!(T,size)
Vector!(T,size) * Vector!(T,size)
Vector!(T,size) / Vector!(T,size)
Vector!(T,size) + T
Vector!(T,size) - T
Vector!(T,size) * T
Vector!(T,size) / T
Vector!(T,size) % T
T * Vector!(T,size)
T = Vector!(T,size)index
Vector!(T,size)index = T
Vector!(T,size) += Vector!(T,size)
Vector!(T,size) -= Vector!(T,size)
Vector!(T,size) *= Vector!(T,size)
Vector!(T,size) /= Vector!(T,size)
Vector!(T,size) += T
Vector!(T,size) -= T
Vector!(T,size) *= T
Vector!(T,size) /= T
Vector!(T,size) %= T
T[] = Vector!(T,size)index1..index2
T = Vector!(T,size)[]
Vector!(T,size)index1..index2 = T
Vector!(T,size)[] = T
-Vector!(T,size)
+Vector!(T,size)
Return true if all components are zero
Get vector length
Get vector length squared
Return normalized copy
Convert to string
Swizzling
Vector components
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 }
Vector representation