forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathHelper.cs
326 lines (294 loc) · 14.2 KB
/
MathHelper.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Contains commonly used precalculated values and mathematical operations.
/// </summary>
public static class MathHelper
{
/// <summary>
/// Represents the mathematical constant e(2.71828175).
/// </summary>
public const float E = MathF.E;
/// <summary>
/// Represents the log base ten of e(0.4342945).
/// </summary>
public const float Log10E = 0.4342945f;
/// <summary>
/// Represents the log base two of e(1.442695).
/// </summary>
public const float Log2E = 1.442695f;
/// <summary>
/// Represents the value of pi(3.14159274).
/// </summary>
public const float Pi = MathF.PI;
/// <summary>
/// Represents the value of pi divided by two(1.57079637).
/// </summary>
public const float PiOver2 = (float)(Math.PI / 2.0);
/// <summary>
/// Represents the value of pi divided by four(0.7853982).
/// </summary>
public const float PiOver4 = (float)(Math.PI / 4.0);
/// <summary>
/// Represents the value of pi times two(6.28318548).
/// </summary>
public const float TwoPi = (float)(Math.PI * 2.0);
/// <summary>
/// Represents the value of pi times two(6.28318548).
/// This is an alias of TwoPi.
/// </summary>
public const float Tau = TwoPi;
/// <summary>
/// Returns the Cartesian coordinate for one axis of a point that is defined by a given triangle and two normalized barycentric (areal) coordinates.
/// </summary>
/// <param name="value1">The coordinate on one axis of vertex 1 of the defining triangle.</param>
/// <param name="value2">The coordinate on the same axis of vertex 2 of the defining triangle.</param>
/// <param name="value3">The coordinate on the same axis of vertex 3 of the defining triangle.</param>
/// <param name="amount1">The normalized barycentric (areal) coordinate b2, equal to the weighting factor for vertex 2, the coordinate of which is specified in value2.</param>
/// <param name="amount2">The normalized barycentric (areal) coordinate b3, equal to the weighting factor for vertex 3, the coordinate of which is specified in value3.</param>
/// <returns>Cartesian coordinate of the specified point with respect to the axis being used.</returns>
public static float Barycentric(float value1, float value2, float value3, float amount1, float amount2)
{
return value1 + (value2 - value1) * amount1 + (value3 - value1) * amount2;
}
/// <summary>
/// Performs a Catmull-Rom interpolation using the specified positions.
/// </summary>
/// <param name="value1">The first position in the interpolation.</param>
/// <param name="value2">The second position in the interpolation.</param>
/// <param name="value3">The third position in the interpolation.</param>
/// <param name="value4">The fourth position in the interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>A position that is the result of the Catmull-Rom interpolation.</returns>
public static float CatmullRom(float value1, float value2, float value3, float value4, float amount)
{
// Using formula from http://www.mvps.org/directx/articles/catmull/
// Internally using doubles not to lose precission
double amountSquared = amount * amount;
double amountCubed = amountSquared * amount;
return (float)(0.5 * (2.0 * value2 +
(value3 - value1) * amount +
(2.0 * value1 - 5.0 * value2 + 4.0 * value3 - value4) * amountSquared +
(3.0 * value2 - value1 - 3.0 * value3 + value4) * amountCubed));
}
/// <summary>
/// Restricts a value to be within a specified range.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value. If <c>value</c> is less than <c>min</c>, <c>min</c> will be returned.</param>
/// <param name="max">The maximum value. If <c>value</c> is greater than <c>max</c>, <c>max</c> will be returned.</param>
/// <returns>The clamped value.</returns>
public static float Clamp(float value, float min, float max)
{
// First we check to see if we're greater than the max
value = (value > max) ? max : value;
// Then we check to see if we're less than the min.
value = (value < min) ? min : value;
// There's no check to see if min > max.
return value;
}
/// <summary>
/// Restricts a value to be within a specified range.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value. If <c>value</c> is less than <c>min</c>, <c>min</c> will be returned.</param>
/// <param name="max">The maximum value. If <c>value</c> is greater than <c>max</c>, <c>max</c> will be returned.</param>
/// <returns>The clamped value.</returns>
public static int Clamp(int value, int min, int max)
{
value = (value > max) ? max : value;
value = (value < min) ? min : value;
return value;
}
/// <summary>
/// Calculates the absolute value of the difference of two values.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Source value.</param>
/// <returns>Distance between the two values.</returns>
public static float Distance(float value1, float value2)
{
return Math.Abs(value1 - value2);
}
/// <summary>
/// Performs a Hermite spline interpolation.
/// </summary>
/// <param name="value1">Source position.</param>
/// <param name="tangent1">Source tangent.</param>
/// <param name="value2">Source position.</param>
/// <param name="tangent2">Source tangent.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>The result of the Hermite spline interpolation.</returns>
public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)
{
// All transformed to double not to lose precission
// Otherwise, for high numbers of param:amount the result is NaN instead of Infinity
double v1 = value1, v2 = value2, t1 = tangent1, t2 = tangent2, s = amount, result;
double sCubed = s * s * s;
double sSquared = s * s;
if (amount == 0f)
result = value1;
else if (amount == 1f)
result = value2;
else
result = (2 * v1 - 2 * v2 + t2 + t1) * sCubed +
(3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared +
t1 * s +
v1;
return (float)result;
}
/// <summary>
/// Linearly interpolates between two values.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Destination value.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of value2.</param>
/// <returns>Interpolated value.</returns>
/// <remarks>This method performs the linear interpolation based on the following formula:
/// <code>value1 + (value2 - value1) * amount</code>.
/// Passing amount a value of 0 will cause value1 to be returned, a value of 1 will cause value2 to be returned.
/// See <see cref="MathHelper.LerpPrecise"/> for a less efficient version with more precision around edge cases.
/// </remarks>
public static float Lerp(float value1, float value2, float amount)
{
return value1 + (value2 - value1) * amount;
}
/// <summary>
/// Linearly interpolates between two values.
/// This method is a less efficient, more precise version of <see cref="MathHelper.Lerp"/>.
/// See remarks for more info.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Destination value.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of value2.</param>
/// <returns>Interpolated value.</returns>
/// <remarks>This method performs the linear interpolation based on the following formula:
/// <code>((1 - amount) * value1) + (value2 * amount)</code>.
/// Passing amount a value of 0 will cause value1 to be returned, a value of 1 will cause value2 to be returned.
/// This method does not have the floating point precision issue that <see cref="MathHelper.Lerp"/> has.
/// i.e. If there is a big gap between value1 and value2 in magnitude (e.g. value1=10000000000000000, value2=1),
/// right at the edge of the interpolation range (amount=1), <see cref="MathHelper.Lerp"/> will return 0 (whereas it should return 1).
/// This also holds for value1=10^17, value2=10; value1=10^18,value2=10^2... so on.
/// For an in depth explanation of the issue, see below references:
/// Relevant Wikipedia Article: https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support
/// Relevant StackOverflow Answer: http://stackoverflow.com/questions/4353525/floating-point-linear-interpolation#answer-23716956
/// </remarks>
public static float LerpPrecise(float value1, float value2, float amount)
{
return ((1 - amount) * value1) + (value2 * amount);
}
/// <summary>
/// Returns the greater of two values.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Source value.</param>
/// <returns>The greater value.</returns>
public static float Max(float value1, float value2)
{
return value1 > value2 ? value1 : value2;
}
/// <summary>
/// Returns the greater of two values.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Source value.</param>
/// <returns>The greater value.</returns>
public static int Max(int value1, int value2)
{
return value1 > value2 ? value1 : value2;
}
/// <summary>
/// Returns the lesser of two values.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Source value.</param>
/// <returns>The lesser value.</returns>
public static float Min(float value1, float value2)
{
return value1 < value2 ? value1 : value2;
}
/// <summary>
/// Returns the lesser of two values.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Source value.</param>
/// <returns>The lesser value.</returns>
public static int Min(int value1, int value2)
{
return value1 < value2 ? value1 : value2;
}
/// <summary>
/// Interpolates between two values using a cubic equation.
/// </summary>
/// <param name="value1">Source value.</param>
/// <param name="value2">Source value.</param>
/// <param name="amount">Weighting value.</param>
/// <returns>Interpolated value.</returns>
public static float SmoothStep(float value1, float value2, float amount)
{
// It is expected that 0 < amount < 1
// If amount < 0, return value1
// If amount > 1, return value2
float result = MathHelper.Clamp(amount, 0f, 1f);
result = MathHelper.Hermite(value1, 0f, value2, 0f, result);
return result;
}
/// <summary>
/// Converts radians to degrees.
/// </summary>
/// <param name="radians">The angle in radians.</param>
/// <returns>The angle in degrees.</returns>
/// <remarks>
/// This method uses double precission internally,
/// though it returns single float
/// Factor = 180 / pi
/// </remarks>
public static float ToDegrees(float radians)
{
return (float)(radians * 57.295779513082320876798154814105);
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
/// <param name="degrees">The angle in degrees.</param>
/// <returns>The angle in radians.</returns>
/// <remarks>
/// This method uses double precission internally,
/// though it returns single float
/// Factor = pi / 180
/// </remarks>
public static float ToRadians(float degrees)
{
return (float)(degrees * 0.017453292519943295769236907684886);
}
/// <summary>
/// Reduces a given angle to a value between π and -π.
/// </summary>
/// <param name="angle">The angle to reduce, in radians.</param>
/// <returns>The new angle, in radians.</returns>
public static float WrapAngle(float angle)
{
if ((angle > -Pi) && (angle <= Pi))
return angle;
angle %= TwoPi;
if (angle <= -Pi)
return angle + TwoPi;
if (angle > Pi)
return angle - TwoPi;
return angle;
}
/// <summary>
/// Determines if value is powered by two.
/// </summary>
/// <param name="value">A value.</param>
/// <returns><c>true</c> if <c>value</c> is powered by two; otherwise <c>false</c>.</returns>
public static bool IsPowerOfTwo(int value)
{
return (value > 0) && ((value & (value - 1)) == 0);
}
}
}