123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using System;
- namespace CommonLang.Geometry
- {
-
-
-
- public static class MathHelper
- {
-
-
-
- public const float E = (float)Math.E;
-
-
-
- public const float Log10E = 0.4342945f;
-
-
-
- public const float Log2E = 1.442695f;
-
-
-
- public const float Pi = (float)Math.PI;
-
-
-
- public const float PiOver2 = (float)(Math.PI / 2.0);
-
-
-
- public const float PiOver4 = (float)(Math.PI / 4.0);
-
-
-
- public const float TwoPi = (float)(Math.PI * 2.0);
-
-
-
-
-
-
-
-
-
- public static float Barycentric(float value1, float value2, float value3, float amount1, float amount2)
- {
- return value1 + (value2 - value1) * amount1 + (value3 - value1) * amount2;
- }
-
-
-
-
-
-
-
-
-
- public static float CatmullRom(float value1, float value2, float value3, float value4, float amount)
- {
-
-
- 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));
- }
-
-
-
-
-
-
-
- public static float Clamp(float value, float min, float max)
- {
-
- value = (value > max) ? max : value;
-
- value = (value < min) ? min : value;
-
- return value;
- }
-
-
-
-
-
-
-
- public static int Clamp(int value, int min, int max)
- {
- value = (value > max) ? max : value;
- value = (value < min) ? min : value;
- return value;
- }
-
-
-
-
-
-
- public static float Distance(float value1, float value2)
- {
- return Math.Abs(value1 - value2);
- }
-
-
-
-
-
-
-
-
-
- public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)
- {
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static float Lerp(float value1, float value2, float amount)
- {
- return value1 + (value2 - value1) * amount;
- }
-
-
-
-
-
-
- public static float Max(float value1, float value2)
- {
- return value1 > value2 ? value1 : value2;
- }
-
-
-
-
-
-
- public static int Max(int value1, int value2)
- {
- return value1 > value2 ? value1 : value2;
- }
-
-
-
-
-
-
- public static float Min(float value1, float value2)
- {
- return value1 < value2 ? value1 : value2;
- }
-
-
-
-
-
-
- public static int Min(int value1, int value2)
- {
- return value1 < value2 ? value1 : value2;
- }
-
-
-
-
-
-
-
- public static float SmoothStep(float value1, float value2, float amount)
- {
-
-
-
- float result = MathHelper.Clamp(amount, 0f, 1f);
- result = MathHelper.Hermite(value1, 0f, value2, 0f, result);
- return result;
- }
-
-
-
-
-
-
-
-
-
-
- public static float ToDegrees(float radians)
- {
- return (float)(radians * 57.295779513082320876798154814105);
- }
-
-
-
-
-
-
-
-
-
-
- public static float ToRadians(float degrees)
- {
- return (float)(degrees * 0.017453292519943295769236907684886);
- }
-
-
-
-
-
- public static float WrapAngle(float angle)
- {
- angle = (float)Math.IEEERemainder((double)angle, 6.2831854820251465);
- if (angle <= -3.14159274f)
- {
- angle += 6.28318548f;
- }
- else
- {
- if (angle > 3.14159274f)
- {
- angle -= 6.28318548f;
- }
- }
- return angle;
- }
-
-
-
-
-
- public static bool IsPowerOfTwo(int value)
- {
- return (value > 0) && ((value & (value - 1)) == 0);
- }
- }
- }
|