Jypeli 10
The simple game programming library
MathHelper.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26
27#if UseDouble
28using Scalar = System.Double;
29#else
30using Scalar = System.Single;
31#endif
32using System;
33
34
35namespace AdvanceMath
36{
37 public static class MathHelper
38 {
39 #region consts
40 public const Scalar E = (Scalar)System.Math.E;
41 public const Scalar Pi = (Scalar)System.Math.PI;
42 public const Scalar TwoPi = (Scalar)(System.Math.PI * 2);
43 public const Scalar PiOver2 = (Scalar)(System.Math.PI / 2);
44 public const Scalar PiOver4 = (Scalar)(System.Math.PI / 4);
45 public const Scalar ThreePiOver2 = (Scalar)((3 * System.Math.PI) / 2);
46 public const Scalar RadiansPerDegree = (Scalar)(System.Math.PI / 180);
47 public const Scalar DegreesPerRadian = (Scalar)(180 / System.Math.PI);
48 public const Scalar Tolerance = 0.000000001f;
49
50 public const Scalar Epsilon = 1e-03f;
51
52 internal static Scalar Two = 2;
53 #endregion
54 #region methods
55
56 public static Scalar Lerp(Scalar left, Scalar right, Scalar amount)
57 {
58 return (right - left) * amount + left;
59 }
60 public static void Lerp(ref Scalar left, ref Scalar right, ref Scalar amount, out Scalar result)
61 {
62 result = (right - left) * amount + left;
63 }
64
65 public static Scalar CatmullRom(Scalar value1, Scalar value2, Scalar value3, Scalar value4, Scalar amount)
66 {
67 Scalar amountSq = amount * amount;
68 Scalar amountCu = amountSq * amount;
69 return
70 0.5f * ((2 * value2) +
71 (-value1 + value3) * amount +
72 (2 * value1 - 5 * value2 + 4 * value3 - value4) * amountSq +
73 (-value1 + 3 * value2 - 3 * value3 + value4) * amountCu);
74 }
75 public static void CatmullRom(ref Scalar value1, ref Scalar value2, ref Scalar value3, ref Scalar value4, Scalar amount, out Scalar result)
76 {
77 Scalar amountSq = amount * amount;
78 Scalar amountCu = amountSq * amount;
79 result =
80 0.5f * ((2 * value2) +
81 (-value1 + value3) * amount +
82 (2 * value1 - 5 * value2 + 4 * value3 - value4) * amountSq +
83 (-value1 + 3 * value2 - 3 * value3 + value4) * amountCu);
84 }
85
86 internal static void HermiteHelper(Scalar amount, out Scalar h1, out Scalar h2, out Scalar h3, out Scalar h4)
87 {
88 Scalar wf2 = amount * amount;
89 Scalar wf3 = wf2 * amount;
90 Scalar wf3t2 = 2 * wf3;
91 Scalar wf2t3 = 3 * wf2;
92 h1 = wf3t2 - wf2t3 + 1;
93 h2 = wf2t3 - wf3t2;
94 h3 = wf3 - 2 * wf2 + amount;
95 h4 = wf3 - wf2;
96 }
97 public static Scalar Hermite(Scalar value1, Scalar tangent1, Scalar value2, Scalar tangent2, Scalar amount)
98 {
99 Scalar result;
100 Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
101 return result;
102 }
103 public static void Hermite(ref Scalar value1, ref Scalar tangent1, ref Scalar value2, ref Scalar tangent2, Scalar amount, out Scalar result)
104 {
105 Scalar h1, h2, h3, h4;
106 MathHelper.HermiteHelper(amount, out h1, out h2, out h3, out h4);
107 result = h1 * value1 + h2 * value2 + h3 * tangent1 + h4 * tangent2;
108 }
109
110
111 public static Scalar Clamp(Scalar value, Scalar min, Scalar max)
112 {
113 return (value < min) ? (min) : ((value > max) ? (max) : (value));
114 }
115 public static void Clamp(ref Scalar value, ref Scalar min, ref Scalar max, out Scalar result)
116 {
117 result = (value < min) ? (min) : ((value > max) ? (max) : (value));
118 }
119
120
121 public static void Sort(Scalar value1, Scalar value2, out Scalar max, out Scalar min)
122 {
123 if (value1 > value2)
124 {
125 max = value1;
126 min = value2;
127 }
128 else
129 {
130 max = value2;
131 min = value1;
132 }
133 }
134
135
143 public static Scalar WrapClamp(Scalar value, Scalar min, Scalar max)
144 {
145 if (min <= value && value < max) { return value; }
146 Scalar rem = (value - min) % (max - min);
147 return rem + ((rem < 0) ? (max) : (min));
148 }
156 public static void WrapClamp(ref Scalar value, ref Scalar min, ref Scalar max, out Scalar result)
157 {
158 if (min <= value && value < max) { result = value; return; }
159 Scalar rem = (value - min) % (max - min);
160 result = rem + ((rem < 0) ? (max) : (min));
161 }
162
163
164 public static Scalar ClampAngle(Scalar angle)
165 {
166 if (-Pi <= angle && angle < Pi) { return angle; }
167 Scalar rem = (angle + Pi) % (TwoPi);
168 return rem + ((rem < 0) ? (Pi) : (-Pi));
169 }
170 public static void ClampAngle(ref Scalar angle)
171 {
172 if (-Pi <= angle && angle < Pi) { return; }
173 Scalar rem = (angle + Pi) % (TwoPi);
174 angle = rem + ((rem < 0) ? (Pi) : (-Pi));
175 }
176 public static void ClampAngle(ref Scalar angle, out Scalar result)
177 {
178 if (-Pi <= angle && angle < Pi) { result = angle; return; }
179 Scalar rem = (angle + Pi) % (TwoPi);
180 result = rem + ((rem < 0) ? (Pi) : (-Pi));
181 }
182
183 public static Scalar AngleSubtract(Scalar angle1, Scalar angle2)
184 {
185 return ClampAngle(angle1 - angle2);
186 }
187 public static void AngleSubtract(ref Scalar angle1, ref Scalar angle2, out Scalar result)
188 {
189 result = angle1 - angle2;
190 ClampAngle(ref result);
191 }
192
202 public static bool TrySolveQuadratic(Scalar a, Scalar b, Scalar c, out Scalar plus, out Scalar minus)
203 {
204 if (0 == a)
205 {
206 plus = -c / b;
207 minus = plus;
208 return true;
209 }
210 c = (b * b) - (4 * a * c);
211 if (0 <= c)
212 {
213 c = Sqrt(c);
214 a = .5f / a;
215 plus = ((c - b) * a);
216 minus = ((-c - b) * a);
217 return true;
218 }
219 plus = 0;
220 minus = 0;
221 return false;
222 }
223 public static Scalar InvSqrt(Scalar number)
224 {
225 return 1 / Sqrt(number);
226 }
227
228 public static Scalar Max(params Scalar[] vals)
229 {
230 if (vals == null) { throw new ArgumentNullException("vals"); }
231 if (vals.Length == 0) { throw new ArgumentException("There must be at least one value to compare", "vals"); }
232 Scalar max = vals[0];
233 if (Scalar.IsNaN(max)) { return max; }
234 for (int i = 1; i < vals.Length; i++)
235 {
236 Scalar val = vals[i];
237 if (val > max) { max = val; }
238 else if (Scalar.IsNaN(val)) { return val; }
239 }
240 return max;
241 }
242 public static Scalar Min(params Scalar[] vals)
243 {
244 if (vals == null) { throw new ArgumentNullException("vals"); }
245 if (vals.Length == 0) { throw new ArgumentException("There must be at least one value to compare", "vals"); }
246 Scalar min = vals[0];
247 if (Scalar.IsNaN(min)) { return min; }
248 for (int i = 1; i < vals.Length; i++)
249 {
250 Scalar val = vals[i];
251 if (val < min) { min = val; }
252 else if (Scalar.IsNaN(val)) { return val; }
253 }
254 return min;
255 }
256
257 public static bool PointInTri2D(Vector2D point, Vector2D a, Vector2D b, Vector2D c)
258 {
259 Vector2D vect1, vect2;
260 Scalar temp;
261 Vector2D.Subtract(ref b, ref a, out vect1);
262 Vector2D.Subtract(ref point, ref b, out vect2);
263 Vector2D.ZCross(ref vect1, ref vect2, out temp);
264 bool bClockwise = temp >= 0;
265 Vector2D.Subtract(ref c, ref b, out vect1);
266 Vector2D.Subtract(ref point, ref c, out vect2);
267 Vector2D.ZCross(ref vect1, ref vect2, out temp);
268 if (temp < 0 ^ bClockwise) { return true; }
269 Vector2D.Subtract(ref a, ref c, out vect1);
270 Vector2D.Subtract(ref point, ref a, out vect2);
271 Vector2D.ZCross(ref vect1, ref vect2, out temp);
272 return temp < 0 ^ bClockwise;
273
274 /* bool bClockwise = (((b - a) ^ (point - b)) >= 0);
275 return !(((((c - b) ^ (point - c)) >= 0) ^ bClockwise) && ((((a - c) ^ (point - a)) >= 0) ^ bClockwise));*/
276 }
282 public static Scalar ToRadians(Scalar degrees)
283 {
284 return degrees * RadiansPerDegree;
285 }
291 public static Scalar ToDegrees(Scalar radians)
292 {
293 return radians * DegreesPerRadian;
294 }
295
296 #region System.Math Methods
297 public static Scalar Acos(Scalar d) { return (Scalar)Math.Acos(d); }
298 public static Scalar Asin(Scalar d) { return (Scalar)Math.Asin(d); }
299 public static Scalar Atan(Scalar d) { return (Scalar)Math.Atan(d); }
300 public static Scalar Atan2(Scalar y, Scalar x) { return (Scalar)Math.Atan2(y, x); }
301 public static Scalar Ceiling(Scalar a) { return (Scalar)Math.Ceiling(a); }
302 public static Scalar Cos(Scalar d) { return (Scalar)Math.Cos(d); }
303 public static Scalar Cosh(Scalar value) { return (Scalar)Math.Cosh(value); }
304 public static Scalar Exp(Scalar d) { return (Scalar)Math.Exp(d); }
305 public static Scalar Floor(Scalar d) { return (Scalar)Math.Floor(d); }
306 public static Scalar IEEERemainder(Scalar x, Scalar y) { return (Scalar)Math.IEEERemainder(x, y); }
307 public static Scalar Log(Scalar d) { return (Scalar)Math.Log(d); }
308 public static Scalar Log10(Scalar d) { return (Scalar)Math.Log10(d); }
309 public static Scalar Pow(Scalar x, Scalar y) { return (Scalar)Math.Pow(x, y); }
310 public static Scalar Round(Scalar a) { return (Scalar)Math.Round(a); }
311 public static Scalar Round(Scalar value, int digits) { return (Scalar)Math.Round(value, digits); }
312 public static Scalar Sin(Scalar a) { return (Scalar)Math.Sin(a); }
313 public static Scalar Sinh(Scalar value) { return (Scalar)Math.Sinh(value); }
314 public static Scalar Sqrt(Scalar d) { return (Scalar)Math.Sqrt(d); }
315 public static Scalar Tan(Scalar a) { return (Scalar)Math.Tan(a); }
316 public static Scalar Tanh(Scalar value) { return (Scalar)Math.Tanh(value); }
317 #endregion
318
319
320 #endregion
321 }
322}
System.Single Scalar
Definition: Clamped.cs:29
System.Single Scalar
Definition: MathHelper.cs:30
static void Hermite(ref Scalar value1, ref Scalar tangent1, ref Scalar value2, ref Scalar tangent2, Scalar amount, out Scalar result)
Definition: MathHelper.cs:103
static Scalar Clamp(Scalar value, Scalar min, Scalar max)
Definition: MathHelper.cs:111
static bool PointInTri2D(Vector2D point, Vector2D a, Vector2D b, Vector2D c)
Definition: MathHelper.cs:257
static void CatmullRom(ref Scalar value1, ref Scalar value2, ref Scalar value3, ref Scalar value4, Scalar amount, out Scalar result)
Definition: MathHelper.cs:75
const Scalar Tolerance
Definition: MathHelper.cs:48
static Scalar Cosh(Scalar value)
Definition: MathHelper.cs:303
static Scalar Acos(Scalar d)
Definition: MathHelper.cs:297
static Scalar Cos(Scalar d)
Definition: MathHelper.cs:302
static Scalar Atan2(Scalar y, Scalar x)
Definition: MathHelper.cs:300
static Scalar Pow(Scalar x, Scalar y)
Definition: MathHelper.cs:309
static Scalar Tanh(Scalar value)
Definition: MathHelper.cs:316
static Scalar Floor(Scalar d)
Definition: MathHelper.cs:305
static void WrapClamp(ref Scalar value, ref Scalar min, ref Scalar max, out Scalar result)
Clamps a value between 2 values, but wraps the value around. So that one plus max would result in one...
Definition: MathHelper.cs:156
static Scalar Ceiling(Scalar a)
Definition: MathHelper.cs:301
static Scalar Max(params Scalar[] vals)
Definition: MathHelper.cs:228
const Scalar Epsilon
Definition: MathHelper.cs:50
static bool TrySolveQuadratic(Scalar a, Scalar b, Scalar c, out Scalar plus, out Scalar minus)
Trys to Solve for x in the equation: (a * (x * x) + b * x + c == 0)
Definition: MathHelper.cs:202
static Scalar Hermite(Scalar value1, Scalar tangent1, Scalar value2, Scalar tangent2, Scalar amount)
Definition: MathHelper.cs:97
static Scalar AngleSubtract(Scalar angle1, Scalar angle2)
Definition: MathHelper.cs:183
static void Sort(Scalar value1, Scalar value2, out Scalar max, out Scalar min)
Definition: MathHelper.cs:121
static Scalar Sin(Scalar a)
Definition: MathHelper.cs:312
static Scalar Lerp(Scalar left, Scalar right, Scalar amount)
Definition: MathHelper.cs:56
static void ClampAngle(ref Scalar angle, out Scalar result)
Definition: MathHelper.cs:176
static Scalar Min(params Scalar[] vals)
Definition: MathHelper.cs:242
static Scalar Atan(Scalar d)
Definition: MathHelper.cs:299
static Scalar Round(Scalar a)
Definition: MathHelper.cs:310
static void Lerp(ref Scalar left, ref Scalar right, ref Scalar amount, out Scalar result)
Definition: MathHelper.cs:60
static void ClampAngle(ref Scalar angle)
Definition: MathHelper.cs:170
static Scalar Sinh(Scalar value)
Definition: MathHelper.cs:313
static Scalar ToRadians(Scalar degrees)
Converts degrees to radians.
Definition: MathHelper.cs:282
static Scalar Log(Scalar d)
Definition: MathHelper.cs:307
static Scalar InvSqrt(Scalar number)
Definition: MathHelper.cs:223
const Scalar PiOver4
Definition: MathHelper.cs:44
static Scalar Round(Scalar value, int digits)
Definition: MathHelper.cs:311
static Scalar Tan(Scalar a)
Definition: MathHelper.cs:315
static Scalar CatmullRom(Scalar value1, Scalar value2, Scalar value3, Scalar value4, Scalar amount)
Definition: MathHelper.cs:65
static Scalar Sqrt(Scalar d)
Definition: MathHelper.cs:314
static void Clamp(ref Scalar value, ref Scalar min, ref Scalar max, out Scalar result)
Definition: MathHelper.cs:115
static Scalar Log10(Scalar d)
Definition: MathHelper.cs:308
const Scalar RadiansPerDegree
Definition: MathHelper.cs:46
static Scalar ToDegrees(Scalar radians)
Converts radians to degrees.
Definition: MathHelper.cs:291
const Scalar TwoPi
Definition: MathHelper.cs:42
static void HermiteHelper(Scalar amount, out Scalar h1, out Scalar h2, out Scalar h3, out Scalar h4)
Definition: MathHelper.cs:86
static Scalar IEEERemainder(Scalar x, Scalar y)
Definition: MathHelper.cs:306
const Scalar ThreePiOver2
Definition: MathHelper.cs:45
const Scalar PiOver2
Definition: MathHelper.cs:43
const Scalar DegreesPerRadian
Definition: MathHelper.cs:47
static Scalar Two
Definition: MathHelper.cs:52
static Scalar ClampAngle(Scalar angle)
Definition: MathHelper.cs:164
static Scalar Asin(Scalar d)
Definition: MathHelper.cs:298
static Scalar WrapClamp(Scalar value, Scalar min, Scalar max)
Clamps a value between 2 values, but wraps the value around. So that one plus max would result in one...
Definition: MathHelper.cs:143
static void AngleSubtract(ref Scalar angle1, ref Scalar angle2, out Scalar result)
Definition: MathHelper.cs:187
static Scalar Exp(Scalar d)
Definition: MathHelper.cs:304
This is the Vector Class.
Definition: Vector2D.cs:50
static Vector2D Subtract(Vector2D left, Vector2D right)
Subtracts 2 Vector2Ds.
Definition: Vector2D.cs:306
static Scalar ZCross(Vector2D left, Vector2D right)
Does a "2D" Cross Product also know as an Outer Product.
Definition: Vector2D.cs:471