-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cs
102 lines (85 loc) · 2.13 KB
/
Vector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Numerics;
namespace SharpEngine
{
public struct Vector
{
public float x, y, z;
public static Vector Forward => new Vector(0, 1);
public static Vector Backward => new Vector(0, -1);
public static Vector Down => new Vector(0, -1);
public static Vector Left => new Vector(-1, 0);
public static Vector Right => new Vector(1, 0);
public static Vector Zero => new Vector(0, 0);
public Vector(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector(float x, float y)
{
this.x = x;
this.y = y;
this.z = 0;
}
public static Vector operator *(Vector v, float f)
{
return new Vector(v.x * f, v.y * f, v.z * f);
}
public static Vector operator *(float f, Vector v)
{
return new Vector(f * v.x, f * v.y, f * v.z);
}
public static Vector operator /(Vector v, float f)
{
return new Vector(v.x / f, v.y / f, v.z / f);
}
public static Vector operator +(Vector lhs, Vector rhs)
{
return new Vector(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
public static Vector operator -(Vector lhs, Vector rhs)
{
return new Vector(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
}
public static Vector operator -(Vector v)
{
return new Vector(-v.x, -v.y, -v.z);
}
public static Vector Max(Vector a, Vector b)
{
return new Vector(MathF.Max(a.x, b.x), MathF.Max(a.y, b.y), MathF.Max(a.z, b.z));
}
public static Vector Min(Vector a, Vector b)
{
return new Vector(MathF.Min(a.x, b.x), MathF.Min(a.y, b.y), MathF.Min(a.z, b.z));
}
public static float Dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
public static float Angle(Vector v)
{
return MathF.Atan2(v.y, v.x);
}
public float GetMagnitude()
{
return MathF.Sqrt(GetSquareMagnitude());
}
public float GetSquareMagnitude()
{
return x * x + y * y + z * z;
}
public Vector Normalize()
{
var magnitude = GetMagnitude();
// DO NOT DIVIDE BY ZERO!
return magnitude > 0 ? this / GetMagnitude() : this;
}
public override string ToString()
{
return $"Vector(X:{this.x}, Y:{this.y}, Z:{this.z}";
}
}
}