1 module prova.math.vector4; 2 3 import prova.math, 4 std.math; 5 6 /// 7 struct Vector4 8 { 9 /// 10 float x = 0; 11 /// 12 float y = 0; 13 /// 14 float z = 0; 15 /// 16 float w = 0; 17 18 /// 19 this(float x, float y, float z, float w) 20 { 21 set(x, y, z, w); 22 } 23 24 /// 25 this(Vector3 vector) 26 { 27 set(vector.x, vector.y, vector.z, 0); 28 } 29 30 /// 31 this(Vector2 vector) 32 { 33 set(vector.x, vector.y, 0, 0); 34 } 35 36 /// Sets the values of x, y, z, and w in a single statement 37 void set(float x, float y, float z, float w) 38 { 39 this.x = x; 40 this.y = y; 41 this.z = z; 42 this.w = w; 43 } 44 45 /// Creates a normalized vector with a random direction 46 static Vector4 random() 47 { 48 Vector4 vector = Vector4( 49 randomF(1), 50 randomF(1), 51 randomF(1), 52 randomF(1) 53 ); 54 55 vector.normalize(); 56 57 return vector; 58 } 59 60 /// Returns a normalized copy of this vector 61 Vector4 getNormalized() const 62 { 63 const float magnitude = getMagnitude(); 64 65 Vector4 result; 66 67 if(magnitude != 0) { 68 result.x = x / magnitude; 69 result.y = y / magnitude; 70 result.z = z / magnitude; 71 result.w = w / magnitude; 72 } 73 74 return result; 75 } 76 77 /// Normalizes the vector 78 void normalize() 79 { 80 const float magnitude = getMagnitude(); 81 82 if(magnitude == 0) 83 return; 84 85 x = x / magnitude; 86 y = y / magnitude; 87 z = z / magnitude; 88 w = w / magnitude; 89 } 90 91 /// Returns the magnitude of the vector 92 float getMagnitude() const 93 { 94 return sqrt(x * x + y * y + z * z + w * w); 95 } 96 97 /** 98 * Sets the magnitude of this vector 99 * 100 * If the previous magnitude is zero, the x value 101 * of the vector will be set to the magnitude 102 */ 103 void setMagnitude(float magnitude) 104 { 105 if(getMagnitude() == 0) { 106 x = magnitude; 107 return; 108 } 109 110 normalize(); 111 112 x *= magnitude; 113 y *= magnitude; 114 z *= magnitude; 115 w *= magnitude; 116 } 117 118 /// Returns the distance between the vectors 119 float distanceTo(Vector4 vector) const 120 { 121 const float a = vector.x - x; 122 const float b = vector.y - y; 123 const float c = vector.z - z; 124 const float d = vector.w - w; 125 126 return sqrt(a * a + b * b + c * c + d * d); 127 } 128 129 /// Returns the squared distance between the vectors 130 float distanceToSquared(Vector4 vector) const 131 { 132 const float a = vector.x - x; 133 const float b = vector.y - y; 134 const float c = vector.z - z; 135 const float d = vector.w - w; 136 137 return a * a + b * b + c * c + d * d; 138 } 139 140 /// Returns the dot product of the two vectors 141 float dot(Vector4 vector) 142 { 143 return x * vector.x + y * vector.y + z * vector.z + w * vector.w; 144 } 145 146 147 // assignment overloading 148 Vector4 opAddAssign(Vector4 vector) 149 { 150 x += vector.x; 151 y += vector.y; 152 z += vector.z; 153 w += vector.w; 154 155 return this; 156 } 157 158 Vector4 opSubAssign(Vector4 vector) 159 { 160 x -= vector.x; 161 y -= vector.y; 162 z -= vector.z; 163 w -= vector.w; 164 165 return this; 166 } 167 168 Vector4 opMulAssign(Vector4 vector) 169 { 170 x *= vector.x; 171 y *= vector.y; 172 z *= vector.z; 173 w *= vector.w; 174 175 return this; 176 } 177 178 Vector4 opMulAssign(float a) 179 { 180 x *= a; 181 y *= a; 182 z *= a; 183 w *= a; 184 185 return this; 186 } 187 188 Vector4 opDivAssign(float a) 189 { 190 x /= a; 191 y /= a; 192 z /= a; 193 w /= a; 194 195 return this; 196 } 197 198 199 // arithmetic overloading 200 Vector4 opAdd(Vector4 vector) const 201 { 202 Vector4 result; 203 result.x = x + vector.x; 204 result.y = y + vector.y; 205 result.z = z + vector.z; 206 result.w = w + vector.w; 207 208 return result; 209 } 210 211 Vector4 opSub(Vector4 vector) const 212 { 213 Vector4 result; 214 result.x = x - vector.x; 215 result.y = y - vector.y; 216 result.z = z - vector.z; 217 result.w = w - vector.w; 218 219 return result; 220 } 221 222 Vector4 opUnary(string s)() const if (s == "-") 223 { 224 Vector4 result; 225 result.x = -x; 226 result.y = -y; 227 result.z = -z; 228 result.w = -w; 229 230 return result; 231 } 232 233 Vector4 opMul(Vector4 vector) const 234 { 235 Vector4 result; 236 result.x = x * vector.x; 237 result.y = y * vector.y; 238 result.z = z * vector.z; 239 result.w = w * vector.w; 240 241 return result; 242 } 243 244 Vector4 opMul(float a) const 245 { 246 Vector4 result; 247 result.x = x * a; 248 result.y = y * a; 249 result.z = z * a; 250 result.w = w * a; 251 252 return result; 253 } 254 255 Vector4 opDiv(float a) const 256 { 257 Vector4 result; 258 result.x = x / a; 259 result.y = y / a; 260 result.z = z / a; 261 result.w = w / a; 262 263 return result; 264 } 265 266 void opAssign(Vector2 vector) 267 { 268 x = vector.x; 269 y = vector.y; 270 z = 0; 271 w = 0; 272 } 273 274 void opAssign(Vector3 vector) 275 { 276 x = vector.x; 277 y = vector.y; 278 z = vector.z; 279 w = 0; 280 } 281 }