-
Notifications
You must be signed in to change notification settings - Fork 8
/
FlatMath.cs
264 lines (227 loc) · 7.15 KB
/
FlatMath.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Runtime.CompilerServices;
using Microsoft.Xna.Framework;
namespace Flat
{
public static class FlatMath
{
public static readonly float TwoPi = 6.283185307179586476925286766559f;
public static readonly float Pi = 3.1415926535897932384626433832795f;
public static readonly float PiOverTwo = 1.5707963267948966192313216916398f;
public static float Min(float a, float b)
{
if (float.IsNaN(a))
{
throw new ArithmeticException("a");
}
if (float.IsNaN(b))
{
throw new ArithmeticException("b");
}
if (a <= b)
{
return a;
}
else
{
return b;
}
}
public static float Max(float a, float b)
{
if (float.IsNaN(a))
{
throw new ArithmeticException("a");
}
if (float.IsNaN(b))
{
throw new ArithmeticException("b");
}
if (a >= b)
{
return a;
}
else
{
return b;
}
}
public static int Clamp(int value, int min, int max)
{
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
else
{
return value;
}
}
public static float Clamp(float value, float min, float max)
{
if (float.IsNaN(min))
{
throw new ArithmeticException("min is NaN.");
}
if (float.IsNaN(max))
{
throw new ArithmeticException("max is NaN");
}
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
else
{
return value;
}
}
public static double Clamp(double value, double min, double max)
{
if (double.IsNaN(min))
{
throw new ArithmeticException("min is NaN.");
}
if (double.IsNaN(max))
{
throw new ArithmeticException("max is NaN");
}
if (min > max)
{
FlatUtil.Swap(ref min, ref max);
}
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
else
{
return value;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Normalize(Vector2 value)
{
float invLen = 1f / MathF.Sqrt(value.X * value.X + value.Y * value.Y);
return new Vector2(value.X * invLen, value.Y * invLen);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Normalize(ref float x, ref float y)
{
float invLen = 1f / MathF.Sqrt(x * x + y * y);
x *= invLen;
y *= invLen;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Normalize(float x, float y)
{
float invLen = 1f / MathF.Sqrt(x * x + y * y);
return new Vector2(x * invLen, y * invLen);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Length(Vector2 value)
{
return MathF.Sqrt(value.X * value.X + value.Y * value.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Length(float x, float y)
{
return MathF.Sqrt(x * x + y * y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float LengthSquared(Vector2 value)
{
return value.X * value.X + value.Y * value.Y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float LengthSquared(float x, float y)
{
return x * x + y * y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(Vector2 a, Vector2 b)
{
float dx = a.X - b.X;
float dy = a.Y - b.Y;
return MathF.Sqrt(dx * dx + dy * dy);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistanceSquared(Vector2 a, Vector2 b)
{
float dx = a.X - b.X;
float dy = a.Y - b.Y;
return dx * dx + dy * dy;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(float ax, float ay, float bx, float by)
{
float dx = ax - bx;
float dy = ay - by;
return MathF.Sqrt(dx * dx + dy * dy);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DistanceSquared(float ax, float ay, float bx, float by)
{
float dx = ax - bx;
float dy = ay - by;
return dx * dx + dy * dy;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(Vector2 a, Vector2 b)
{
return a.X * b.X + a.Y * b.Y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(float ax, float ay, float bx, float by)
{
return ax * bx + ay * by;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cross(Vector2 a, Vector2 b)
{
return a.X * b.Y - a.Y * b.X;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cross(float ax, float ay, float bx, float by)
{
return ax * by - ay * bx;
}
public static Vector2 FindArithmeticMean(Vector2[] vertices)
{
float sx = 0f;
float sy = 0f;
for (int i = 0; i < vertices.Length; i++)
{
Vector2 v = vertices[i];
sx += v.X;
sy += v.Y;
}
float invArrayLen = 1f / vertices.Length;
return new Vector2(sx * invArrayLen, sy * invArrayLen);
}
public static Vector2 FindArithmeticMean(Vector2 min, Vector2 max)
{
return new Vector2((min.X + max.X) * 0.5f, (min.Y + max.Y) * 0.5f);
}
}
}