Falco Engine 3.9.0.1 (beta)
Vector4.cs
Go to the documentation of this file.
1
2using System;
3using System.Globalization;
4
5namespace FalcoEngine
6{
7 public struct Vector4
8 {
9 public float x;
10 public float y;
11 public float z;
12 public float w;
13
21 public Vector4(float x, float y, float z, float w)
22 {
23 this.x = x;
24 this.y = y;
25 this.z = z;
26 this.w = w;
27 }
28
35 public Vector4(float x, float y, float z)
36 {
37 this.x = x;
38 this.y = y;
39 this.z = z;
40 w = 0f;
41 }
42
48 public Vector4(float x, float y)
49 {
50 this.x = x;
51 this.y = y;
52 z = 0f;
53 w = 0f;
54 }
55
56 private static readonly Vector4 zeroVector = new Vector4(0f, 0f, 0f, 0f);
57
58 private static readonly Vector4 oneVector = new Vector4(1f, 1f, 1f, 1f);
59
60 private static readonly Vector4 positiveInfinityVector = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
61
62 private static readonly Vector4 negativeInfinityVector = new Vector4(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
63
64 public float this[int index]
65 {
66 get
67 {
68 switch (index)
69 {
70 case 0:
71 return x;
72 case 1:
73 return y;
74 case 2:
75 return z;
76 case 3:
77 return w;
78 default:
79 throw new IndexOutOfRangeException("Invalid Vector4 index!");
80 }
81 }
82 set
83 {
84 switch (index)
85 {
86 case 0:
87 x = value;
88 break;
89 case 1:
90 y = value;
91 break;
92 case 2:
93 z = value;
94 break;
95 case 3:
96 w = value;
97 break;
98 default:
99 throw new IndexOutOfRangeException("Invalid Vector4 index!");
100 }
101 }
102 }
103
107 public Vector4 normalized => Normalize(this);
108
112 public float magnitude => (float)Math.Sqrt((double)Dot(this, this));
113
117 public float sqrMagnitude => Dot(this, this);
118
122 public static Vector4 zero => zeroVector;
123
127 public static Vector4 one => oneVector;
128
132 public static Vector4 positiveInfinity => positiveInfinityVector;
133
137 public static Vector4 negativeInfinity => negativeInfinityVector;
138
146 public void Set(float newX, float newY, float newZ, float newW)
147 {
148 x = newX;
149 y = newY;
150 z = newZ;
151 w = newW;
152 }
153
160 public static Vector4 Lerp(Vector4 a, Vector4 b, float t)
161 {
162 t = Mathf.Clamp01(t);
163 return new Vector4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t);
164 }
165
172 public static Vector4 LerpUnclamped(Vector4 a, Vector4 b, float t)
173 {
174 return new Vector4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t);
175 }
176
183 public static Vector4 MoveTowards(Vector4 current, Vector4 target, float maxDistanceDelta)
184 {
185 float num = target.x - current.x;
186 float num2 = target.y - current.y;
187 float num3 = target.z - current.z;
188 float num4 = target.w - current.w;
189 float num5 = num * num + num2 * num2 + num3 * num3 + num4 * num4;
190 if (num5 == 0f || (maxDistanceDelta >= 0f && num5 <= maxDistanceDelta * maxDistanceDelta))
191 {
192 return target;
193 }
194 float num6 = (float)Math.Sqrt((double)num5);
195 return new Vector4(current.x + num / num6 * maxDistanceDelta, current.y + num2 / num6 * maxDistanceDelta, current.z + num3 / num6 * maxDistanceDelta, current.w + num4 / num6 * maxDistanceDelta);
196 }
197
203 public static Vector4 Scale(Vector4 a, Vector4 b)
204 {
205 return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
206 }
207
212 public void Scale(Vector4 scale)
213 {
214 x *= scale.x;
215 y *= scale.y;
216 z *= scale.z;
217 w *= scale.w;
218 }
219
220 public override int GetHashCode()
221 {
222 return x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2) ^ (w.GetHashCode() >> 1);
223 }
224
229 public override bool Equals(object other)
230 {
231 if (!(other is Vector4))
232 {
233 return false;
234 }
235 return Equals((Vector4)other);
236 }
237
238 public bool Equals(Vector4 other)
239 {
240 return x == other.x && y == other.y && z == other.z && w == other.w;
241 }
242
247 public static Vector4 Normalize(Vector4 a)
248 {
249 float num = Magnitude(a);
250 if (num > 1E-05f)
251 {
252 return a / num;
253 }
254 return zero;
255 }
256
260 public void Normalize()
261 {
262 float num = Magnitude(this);
263 if (num > 1E-05f)
264 {
265 this /= num;
266 }
267 else
268 {
269 this = zero;
270 }
271 }
272
278 public static float Dot(Vector4 a, Vector4 b)
279 {
280 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
281 }
282
288 public static Vector4 Project(Vector4 a, Vector4 b)
289 {
290 return b * (Dot(a, b) / Dot(b, b));
291 }
292
298 public static float Distance(Vector4 a, Vector4 b)
299 {
300 return Magnitude(a - b);
301 }
302
303 public static float Magnitude(Vector4 a)
304 {
305 return (float)Math.Sqrt((double)Dot(a, a));
306 }
307
313 public static Vector4 Min(Vector4 lhs, Vector4 rhs)
314 {
315 return new Vector4(Mathf.Min(lhs.x, rhs.x), Mathf.Min(lhs.y, rhs.y), Mathf.Min(lhs.z, rhs.z), Mathf.Min(lhs.w, rhs.w));
316 }
317
323 public static Vector4 Max(Vector4 lhs, Vector4 rhs)
324 {
325 return new Vector4(Mathf.Max(lhs.x, rhs.x), Mathf.Max(lhs.y, rhs.y), Mathf.Max(lhs.z, rhs.z), Mathf.Max(lhs.w, rhs.w));
326 }
327
328 public static Vector4 operator +(Vector4 a, Vector4 b)
329 {
330 return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
331 }
332
333 public static Vector4 operator -(Vector4 a, Vector4 b)
334 {
335 return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
336 }
337
338 public static Vector4 operator -(Vector4 a)
339 {
340 return new Vector4(0f - a.x, 0f - a.y, 0f - a.z, 0f - a.w);
341 }
342
343 public static Vector4 operator *(Vector4 a, float d)
344 {
345 return new Vector4(a.x * d, a.y * d, a.z * d, a.w * d);
346 }
347
348 public static Vector4 operator *(float d, Vector4 a)
349 {
350 return new Vector4(a.x * d, a.y * d, a.z * d, a.w * d);
351 }
352
353 public static Vector4 operator /(Vector4 a, float d)
354 {
355 return new Vector4(a.x / d, a.y / d, a.z / d, a.w / d);
356 }
357
358 public static bool operator ==(Vector4 lhs, Vector4 rhs)
359 {
360 float num = lhs.x - rhs.x;
361 float num2 = lhs.y - rhs.y;
362 float num3 = lhs.z - rhs.z;
363 float num4 = lhs.w - rhs.w;
364 float num5 = num * num + num2 * num2 + num3 * num3 + num4 * num4;
365 return num5 < 9.99999944E-11f;
366 }
367
368 public static bool operator !=(Vector4 lhs, Vector4 rhs)
369 {
370 return !(lhs == rhs);
371 }
372
373 public static implicit operator Vector4(Vector3 v)
374 {
375 return new Vector4(v.x, v.y, v.z, 0f);
376 }
377
378 public static implicit operator Vector3(Vector4 v)
379 {
380 return new Vector3(v.x, v.y, v.z);
381 }
382
383 public static implicit operator Vector4(Vector2 v)
384 {
385 return new Vector4(v.x, v.y, 0f, 0f);
386 }
387
388 public static implicit operator Vector2(Vector4 v)
389 {
390 return new Vector2(v.x, v.y);
391 }
392
397 public override string ToString()
398 {
399 return String.Format("({0:F4}, {1:F4}, {2:F4}, {3:F4})", x, y, z, w);
400 }
401
406 public string ToString(string format)
407 {
408 return String.Format("({0}, {1}, {2}, {3})", x.ToString(format, CultureInfo.InvariantCulture.NumberFormat), y.ToString(format, CultureInfo.InvariantCulture.NumberFormat), z.ToString(format, CultureInfo.InvariantCulture.NumberFormat), w.ToString(format, CultureInfo.InvariantCulture.NumberFormat));
409 }
410
411 public static float SqrMagnitude(Vector4 a)
412 {
413 return Dot(a, a);
414 }
415
416 public float SqrMagnitude()
417 {
418 return Dot(this, this);
419 }
420 }
421}
static float Min(float a, float b)
Definition: Mathf.cs:150
static float Max(float a, float b)
Definition: Mathf.cs:220
static float Clamp01(float value)
Definition: Mathf.cs:436
override bool Equals(object other)
Definition: Vector4.cs:229
string ToString(string format)
Definition: Vector4.cs:406
static Vector4 MoveTowards(Vector4 current, Vector4 target, float maxDistanceDelta)
Definition: Vector4.cs:183
static float Dot(Vector4 a, Vector4 b)
Definition: Vector4.cs:278
override string ToString()
Definition: Vector4.cs:397
static Vector4 positiveInfinity
Definition: Vector4.cs:132
static Vector4 Project(Vector4 a, Vector4 b)
Definition: Vector4.cs:288
Vector4(float x, float y)
Definition: Vector4.cs:48
Vector4(float x, float y, float z, float w)
Definition: Vector4.cs:21
float SqrMagnitude()
Definition: Vector4.cs:416
Vector4 normalized
Definition: Vector4.cs:107
static Vector4 negativeInfinity
Definition: Vector4.cs:137
static Vector4 Scale(Vector4 a, Vector4 b)
Definition: Vector4.cs:203
void Scale(Vector4 scale)
Definition: Vector4.cs:212
bool Equals(Vector4 other)
Definition: Vector4.cs:238
override int GetHashCode()
Definition: Vector4.cs:220
static Vector4 one
Definition: Vector4.cs:127
static Vector4 Min(Vector4 lhs, Vector4 rhs)
Definition: Vector4.cs:313
static Vector4 Normalize(Vector4 a)
Definition: Vector4.cs:247
static Vector4 zero
Definition: Vector4.cs:122
static Vector4 operator-(Vector4 a, Vector4 b)
Definition: Vector4.cs:333
static Vector4 Max(Vector4 lhs, Vector4 rhs)
Definition: Vector4.cs:323
void Set(float newX, float newY, float newZ, float newW)
Definition: Vector4.cs:146
static float Magnitude(Vector4 a)
Definition: Vector4.cs:303
static bool operator!=(Vector4 lhs, Vector4 rhs)
Definition: Vector4.cs:368
static Vector4 operator/(Vector4 a, float d)
Definition: Vector4.cs:353
static bool operator==(Vector4 lhs, Vector4 rhs)
Definition: Vector4.cs:358
static Vector4 operator+(Vector4 a, Vector4 b)
Definition: Vector4.cs:328
static Vector4 Lerp(Vector4 a, Vector4 b, float t)
Definition: Vector4.cs:160
static float SqrMagnitude(Vector4 a)
Definition: Vector4.cs:411
Vector4(float x, float y, float z)
Definition: Vector4.cs:35
static Vector4 LerpUnclamped(Vector4 a, Vector4 b, float t)
Definition: Vector4.cs:172
static float Distance(Vector4 a, Vector4 b)
Definition: Vector4.cs:298
static Vector4 operator*(Vector4 a, float d)
Definition: Vector4.cs:343