-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector2.cs
203 lines (179 loc) · 4.62 KB
/
Vector2.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
namespace Storm;
using Utils;
sealed public class Vector2
{
public float x = 0f;
public float y = 0f;
public readonly static Vector2 Zero = new( 0f );
public readonly static Vector2 One = new( 1f );
public readonly static Vector2 Right = new( 1f, 0f);
public readonly static Vector2 Left = new(-1f, 0f);
public readonly static Vector2 Up = new( 0f, -1f);
public readonly static Vector2 Down = new( 0f, 1f);
#region Constructors
public Vector2()
{
x = 0f;
y = 0f;
}
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public Vector2(float value)
{
this.x = value;
this.y = value;
}
#endregion
#region Helper Functions
public float GetLength()
{
float length = MathF.Sqrt( (x * x) + (y * y) );
if (float.IsNaN(length))
{
return 0;
}
return length;
}
public float GetLengthSquared()
{
return (x * x) + (y * y);
}
public Vector2 SetLength(float length)
{
float currentLength = GetLength();
float x = this.x;
float y = this.y;
if (currentLength != 0)
{
float scale = length / currentLength;
x *= scale;
y *= scale;
}
return new(x, y);
}
public float GetAngle()
{
return MathHelper.RadToDeg(MathF.Atan2(y, x));
}
public void SetAngle(float angle)
{
float l = GetLength();
float radian = MathHelper.DegToRad(angle);
float x = l * MathF.Cos(radian);
float y = l * MathF.Sin(radian);
this.x = x;
this.y = y;
}
public void Normalize()
{
float l = GetLength();
float x = this.x / l;
float y = this.y / l;
if (float.IsNaN(x) || float.IsNaN(y))
{
this.x = 0;
this.y = 0;
return;
}
this.x = x;
this.y = y;
}
public Vector2 Normalized()
{
float l = GetLength();
float x = this.x / l;
float y = this.y / l;
if (float.IsNaN(x) || float.IsNaN(y))
{
return new();
}
return new(x, y);
}
public float Distance(Vector2 to)
{
return (this - to).Abs().GetLength();
}
public float DistanceSquared(Vector2 to)
{
return (this - to).Abs().GetLengthSquared();
}
public Vector2 Abs()
{
return new(MathF.Abs(x), MathF.Abs(y));
}
public float Dot(Vector2 v)
{
return (x * v.x) + (y * v.y);
}
public float Cross(Vector2 v)
{
return (x * v.y) - (y * v.x);
}
public Vector2 Rotated(float angle)
{
float radian = MathHelper.DegToRad(angle);
float cos = MathF.Cos(radian);
float sin = MathF.Sin(radian);
float x = this.x * cos - this.y * sin;
float y = this.x * sin + this.y * cos;
return new Vector2(x, y);
}
public Vector2 Perpendicular()
{
return new Vector2(-y, x);
}
#endregion
#region Operators
public static Vector2 operator +(Vector2 vec1, Vector2 vec2) {
float x = vec1.x + vec2.x;
float y = vec1.y + vec2.y;
return new Vector2(x, y);
}
public static Vector2 operator -(Vector2 vec1, Vector2 vec2) {
float x = vec1.x - vec2.x;
float y = vec1.y - vec2.y;
return new Vector2(x, y);
}
public static Vector2 operator *(Vector2 vec1, Vector2 vec2) {
float x = vec1.x * vec2.x;
float y = vec1.y * vec2.y;
return new Vector2(x, y);
}
public static Vector2 operator /(Vector2 vec1, Vector2 vec2) {
float x = vec1.x / vec2.x;
float y = vec1.y / vec2.y;
return new Vector2(x, y);
}
public static Vector2 operator +(Vector2 vec, float i) {
float x = vec.x + i;
float y = vec.y + i;
return new Vector2(x, y);
}
public static Vector2 operator -(Vector2 vec, float i) {
float x = vec.x - i;
float y = vec.y - i;
return new Vector2(x, y);
}
public static Vector2 operator *(Vector2 vec, float i) {
float x = vec.x * i;
float y = vec.y * i;
return new Vector2(x, y);
}
public static Vector2 operator /(Vector2 vec, float i) {
float x = vec.x / i;
float y = vec.y / i;
return new Vector2(x, y);
}
public static explicit operator Vector2(Point v)
{
return new(v.X, v.Y);
}
#endregion
public override string ToString()
{
return $"( X:{x} , Y:{y} )";
}
}