Falco Engine 3.9.0.1 (beta)
Mathf.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace FalcoEngine
8{
9 public class Mathf
10 {
14 public const float PI = 3.14159274f;
15
19 public const float Infinity = float.PositiveInfinity;
20
24 public const float NegativeInfinity = float.NegativeInfinity;
25
29 public const float Deg2Rad = 0.0174532924f;
30
34 public const float Rad2Deg = 57.29578f;
35
36 public static volatile float FloatMinNormal = 1.17549435E-38f;
37
38 public static volatile float FloatMinDenormal = 1.401298E-45f;
39
40 public static bool IsFlushToZeroEnabled = FloatMinDenormal == 0f;
41
45 public static readonly float Epsilon = (!IsFlushToZeroEnabled) ? FloatMinDenormal : FloatMinNormal;
46
54 public static float Sin(float f)
55 {
56 return (float)Math.Sin((double)f);
57 }
58
66 public static float Cos(float f)
67 {
68 return (float)Math.Cos((double)f);
69 }
70
75 public static float Tan(float f)
76 {
77 return (float)Math.Tan((double)f);
78 }
79
84 public static float Asin(float f)
85 {
86 return (float)Math.Asin((double)f);
87 }
88
93 public static float Acos(float f)
94 {
95 return (float)Math.Acos((double)f);
96 }
97
102 public static float Atan(float f)
103 {
104 return (float)Math.Atan((double)f);
105 }
106
112 public static float Atan2(float y, float x)
113 {
114 return (float)Math.Atan2((double)y, (double)x);
115 }
116
121 public static float Sqrt(float f)
122 {
123 return (float)Math.Sqrt((double)f);
124 }
125
130 public static float Abs(float f)
131 {
132 return Math.Abs(f);
133 }
134
139 public static int Abs(int value)
140 {
141 return Math.Abs(value);
142 }
143
150 public static float Min(float a, float b)
151 {
152 return (!(a < b)) ? b : a;
153 }
154
161 public static float Min(params float[] values)
162 {
163 int num = values.Length;
164 if (num == 0)
165 {
166 return 0f;
167 }
168 float num2 = values[0];
169 for (int i = 1; i < num; i++)
170 {
171 if (values[i] < num2)
172 {
173 num2 = values[i];
174 }
175 }
176 return num2;
177 }
178
185 public static int Min(int a, int b)
186 {
187 return (a >= b) ? b : a;
188 }
189
196 public static int Min(params int[] values)
197 {
198 int num = values.Length;
199 if (num == 0)
200 {
201 return 0;
202 }
203 int num2 = values[0];
204 for (int i = 1; i < num; i++)
205 {
206 if (values[i] < num2)
207 {
208 num2 = values[i];
209 }
210 }
211 return num2;
212 }
213
220 public static float Max(float a, float b)
221 {
222 return (!(a > b)) ? b : a;
223 }
224
231 public static float Max(params float[] values)
232 {
233 int num = values.Length;
234 if (num == 0)
235 {
236 return 0f;
237 }
238 float num2 = values[0];
239 for (int i = 1; i < num; i++)
240 {
241 if (values[i] > num2)
242 {
243 num2 = values[i];
244 }
245 }
246 return num2;
247 }
248
255 public static int Max(int a, int b)
256 {
257 return (a <= b) ? b : a;
258 }
259
266 public static int Max(params int[] values)
267 {
268 int num = values.Length;
269 if (num == 0)
270 {
271 return 0;
272 }
273 int num2 = values[0];
274 for (int i = 1; i < num; i++)
275 {
276 if (values[i] > num2)
277 {
278 num2 = values[i];
279 }
280 }
281 return num2;
282 }
283
289 public static float Pow(float f, float p)
290 {
291 return (float)Math.Pow((double)f, (double)p);
292 }
293
298 public static float Exp(float power)
299 {
300 return (float)Math.Exp((double)power);
301 }
302
308 public static float Log(float f, float p)
309 {
310 return (float)Math.Log((double)f, (double)p);
311 }
312
317 public static float Log(float f)
318 {
319 return (float)Math.Log((double)f);
320 }
321
326 public static float Log10(float f)
327 {
328 return (float)Math.Log10((double)f);
329 }
330
335 public static float Ceil(float f)
336 {
337 return (float)Math.Ceiling((double)f);
338 }
339
344 public static float Floor(float f)
345 {
346 return (float)Math.Floor((double)f);
347 }
348
353 public static float Round(float f)
354 {
355 return (float)Math.Round((double)f);
356 }
357
362 public static int CeilToInt(float f)
363 {
364 return (int)Math.Ceiling((double)f);
365 }
366
371 public static int FloorToInt(float f)
372 {
373 return (int)Math.Floor((double)f);
374 }
375
380 public static int RoundToInt(float f)
381 {
382 return (int)Math.Round((double)f);
383 }
384
389 public static float Sign(float f)
390 {
391 return (!(f >= 0f)) ? (-1f) : 1f;
392 }
393
400 public static float Clamp(float value, float min, float max)
401 {
402 if (value < min)
403 {
404 value = min;
405 }
406 else if (value > max)
407 {
408 value = max;
409 }
410 return value;
411 }
412
419 public static int Clamp(int value, int min, int max)
420 {
421 if (value < min)
422 {
423 value = min;
424 }
425 else if (value > max)
426 {
427 value = max;
428 }
429 return value;
430 }
431
436 public static float Clamp01(float value)
437 {
438 if (value < 0f)
439 {
440 return 0f;
441 }
442 if (value > 1f)
443 {
444 return 1f;
445 }
446 return value;
447 }
448
458 public static float Lerp(float a, float b, float t)
459 {
460 return a + (b - a) * Clamp01(t);
461 }
462
472 public static float LerpUnclamped(float a, float b, float t)
473 {
474 return a + (b - a) * t;
475 }
476
483 public static float LerpAngle(float a, float b, float t)
484 {
485 float num = Repeat(b - a, 360f);
486 if (num > 180f)
487 {
488 num -= 360f;
489 }
490 return a + num * Clamp01(t);
491 }
492
499 public static float MoveTowards(float current, float target, float maxDelta)
500 {
501 if (Abs(target - current) <= maxDelta)
502 {
503 return target;
504 }
505 return current + Sign(target - current) * maxDelta;
506 }
507
514 public static float MoveTowardsAngle(float current, float target, float maxDelta)
515 {
516 float num = DeltaAngle(current, target);
517 if (0f - maxDelta < num && num < maxDelta)
518 {
519 return target;
520 }
521 target = current + num;
522 return MoveTowards(current, target, maxDelta);
523 }
524
531 public static float SmoothStep(float from, float to, float t)
532 {
533 t = Clamp01(t);
534 t = -2f * t * t * t + 3f * t * t;
535 return to * t + from * (1f - t);
536 }
537
538 public static float Gamma(float value, float absmax, float gamma)
539 {
540 bool flag = false;
541 if (value < 0f)
542 {
543 flag = true;
544 }
545 float num = Abs(value);
546 if (num > absmax)
547 {
548 return (!flag) ? num : (0f - num);
549 }
550 float num2 = Pow(num / absmax, gamma) * absmax;
551 return (!flag) ? num2 : (0f - num2);
552 }
553
554 public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
555 {
556 smoothTime = Max(0.0001f, smoothTime);
557 float num = 2f / smoothTime;
558 float num2 = num * deltaTime;
559 float num3 = 1f / (1f + num2 + 0.48f * num2 * num2 + 0.235f * num2 * num2 * num2);
560 float value = current - target;
561 float num4 = target;
562 float num5 = maxSpeed * smoothTime;
563 value = Clamp(value, 0f - num5, num5);
564 target = current - value;
565 float num6 = (currentVelocity + num * value) * deltaTime;
566 currentVelocity = (currentVelocity - num * num6) * num3;
567 float num7 = target + (value + num6) * num3;
568 if (num4 - current > 0f == num7 > num4)
569 {
570 num7 = num4;
571 currentVelocity = (num7 - num4) / deltaTime;
572 }
573 return num7;
574 }
575
576 public static float SmoothDampAngle(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
577 {
578 target = current + DeltaAngle(current, target);
579 return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
580 }
581
587 public static float Repeat(float t, float length)
588 {
589 return Clamp(t - Floor(t / length) * length, 0f, length);
590 }
591
597 public static float PingPong(float t, float length)
598 {
599 t = Repeat(t, length * 2f);
600 return length - Abs(t - length);
601 }
602
609 public static float InverseLerp(float a, float b, float value)
610 {
611 if (a != b)
612 {
613 return Clamp01((value - a) / (b - a));
614 }
615 return 0f;
616 }
617
623 public static float DeltaAngle(float current, float target)
624 {
625 float num = Repeat(target - current, 360f);
626 if (num > 180f)
627 {
628 num -= 360f;
629 }
630 return num;
631 }
632
633 internal static bool LineIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, ref Vector2 result)
634 {
635 float num = p2.x - p1.x;
636 float num2 = p2.y - p1.y;
637 float num3 = p4.x - p3.x;
638 float num4 = p4.y - p3.y;
639 float num5 = num * num4 - num2 * num3;
640 if (num5 == 0f)
641 {
642 return false;
643 }
644 float num6 = p3.x - p1.x;
645 float num7 = p3.y - p1.y;
646 float num8 = (num6 * num4 - num7 * num3) / num5;
647 result = new Vector2(p1.x + num8 * num, p1.y + num8 * num2);
648 return true;
649 }
650
651 internal static bool LineSegmentIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, ref Vector2 result)
652 {
653 float num = p2.x - p1.x;
654 float num2 = p2.y - p1.y;
655 float num3 = p4.x - p3.x;
656 float num4 = p4.y - p3.y;
657 float num5 = num * num4 - num2 * num3;
658 if (num5 == 0f)
659 {
660 return false;
661 }
662 float num6 = p3.x - p1.x;
663 float num7 = p3.y - p1.y;
664 float num8 = (num6 * num4 - num7 * num3) / num5;
665 if (num8 < 0f || num8 > 1f)
666 {
667 return false;
668 }
669 float num9 = (num6 * num2 - num7 * num) / num5;
670 if (num9 < 0f || num9 > 1f)
671 {
672 return false;
673 }
674 result = new Vector2(p1.x + num8 * num, p1.y + num8 * num2);
675 return true;
676 }
677
678 internal static long RandomToLong(System.Random r)
679 {
680 byte[] array = new byte[8];
681 r.NextBytes(array);
682 return (long)(BitConverter.ToUInt64(array, 0) & 0x7FFFFFFFFFFFFFFF);
683 }
684
685 public static bool Approximately(float a, float b)
686 {
687 return Abs(b - a) < Max(1E-06f * Max(Abs(a), Abs(b)), Epsilon * 8f);
688 }
689 }
690}
static int Clamp(int value, int min, int max)
Definition: Mathf.cs:419
static float Acos(float f)
Definition: Mathf.cs:93
static float Tan(float f)
Definition: Mathf.cs:75
static float PingPong(float t, float length)
Definition: Mathf.cs:597
const float NegativeInfinity
Definition: Mathf.cs:24
static int Min(int a, int b)
Definition: Mathf.cs:185
static float Round(float f)
Definition: Mathf.cs:353
static float Min(float a, float b)
Definition: Mathf.cs:150
const float PI
Definition: Mathf.cs:14
static int Max(int a, int b)
Definition: Mathf.cs:255
static float Max(params float[] values)
Definition: Mathf.cs:231
static float Min(params float[] values)
Definition: Mathf.cs:161
static float Ceil(float f)
Definition: Mathf.cs:335
static float Exp(float power)
Definition: Mathf.cs:298
static float Cos(float f)
Definition: Mathf.cs:66
static float Floor(float f)
Definition: Mathf.cs:344
static float MoveTowards(float current, float target, float maxDelta)
Definition: Mathf.cs:499
const float Rad2Deg
Definition: Mathf.cs:34
static float Clamp(float value, float min, float max)
Definition: Mathf.cs:400
static int Max(params int[] values)
Definition: Mathf.cs:266
static float Repeat(float t, float length)
Definition: Mathf.cs:587
const float Deg2Rad
Definition: Mathf.cs:29
static float Max(float a, float b)
Definition: Mathf.cs:220
static int FloorToInt(float f)
Definition: Mathf.cs:371
static float Abs(float f)
Definition: Mathf.cs:130
static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Definition: Mathf.cs:554
static float Pow(float f, float p)
Definition: Mathf.cs:289
static float LerpAngle(float a, float b, float t)
Definition: Mathf.cs:483
static float Asin(float f)
Definition: Mathf.cs:84
static float MoveTowardsAngle(float current, float target, float maxDelta)
Definition: Mathf.cs:514
static int Abs(int value)
Definition: Mathf.cs:139
static float Lerp(float a, float b, float t)
Definition: Mathf.cs:458
static float Sqrt(float f)
Definition: Mathf.cs:121
static float SmoothStep(float from, float to, float t)
Definition: Mathf.cs:531
static float Sin(float f)
Definition: Mathf.cs:54
static volatile float FloatMinNormal
Definition: Mathf.cs:36
static int RoundToInt(float f)
Definition: Mathf.cs:380
static float Log10(float f)
Definition: Mathf.cs:326
static float DeltaAngle(float current, float target)
Definition: Mathf.cs:623
static float Gamma(float value, float absmax, float gamma)
Definition: Mathf.cs:538
static float Log(float f, float p)
Definition: Mathf.cs:308
static float Atan2(float y, float x)
Definition: Mathf.cs:112
static bool Approximately(float a, float b)
Definition: Mathf.cs:685
static float Log(float f)
Definition: Mathf.cs:317
static float Atan(float f)
Definition: Mathf.cs:102
static int Min(params int[] values)
Definition: Mathf.cs:196
static float Clamp01(float value)
Definition: Mathf.cs:436
static readonly float Epsilon
Definition: Mathf.cs:45
static int CeilToInt(float f)
Definition: Mathf.cs:362
static bool IsFlushToZeroEnabled
Definition: Mathf.cs:40
static float LerpUnclamped(float a, float b, float t)
Definition: Mathf.cs:472
const float Infinity
Definition: Mathf.cs:19
static float SmoothDampAngle(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Definition: Mathf.cs:576
static float Sign(float f)
Definition: Mathf.cs:389
static float InverseLerp(float a, float b, float value)
Definition: Mathf.cs:609
static volatile float FloatMinDenormal
Definition: Mathf.cs:38