|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Drawing; |
| 6 | + |
| 7 | +namespace System.Windows.Forms |
| 8 | +{ |
| 9 | + public class MathHelper |
| 10 | + { |
| 11 | + public static float CatmullRom(float value1, float value2, float value3, float value4, float amount) |
| 12 | + { |
| 13 | + // originaly from https://github.com/mono/MonoGame/ |
| 14 | + // Using formula from http://www.mvps.org/directx/articles/catmull/ |
| 15 | + // Internally using doubles not to lose precission |
| 16 | + double amountSquared = amount * amount; |
| 17 | + double amountCubed = amountSquared * amount; |
| 18 | + return (float)(0.5 * (2.0 * value2 + |
| 19 | + (value3 - value1) * amount + |
| 20 | + (2.0 * value1 - 5.0 * value2 + 4.0 * value3 - value4) * amountSquared + |
| 21 | + (3.0 * value2 - value1 - 3.0 * value3 + value4) * amountCubed)); |
| 22 | + } |
| 23 | + public static int Clamp(int value, int min, int max) |
| 24 | + { |
| 25 | + return (value > max ? max : value) < min ? min : value; |
| 26 | + } |
| 27 | + public static float Clamp(float value, float min, float max) |
| 28 | + { |
| 29 | + return (value > max ? max : value) < min ? min : value; |
| 30 | + } |
| 31 | + public static ColorF ColorLerp(ColorF from, Color to, float speed) |
| 32 | + { |
| 33 | + if (from == to) return from; |
| 34 | + var r = FloatLerp(from.R, to.R, speed); |
| 35 | + var g = FloatLerp(from.G, to.G, speed); |
| 36 | + var b = FloatLerp(from.B, to.B, speed); |
| 37 | + var a = FloatLerp(from.A, to.A, speed); |
| 38 | + return ColorF.FromArgb(a, r, g, b); |
| 39 | + } |
| 40 | + public static double DistanceD(float x1, float y1, float x2, float y2) |
| 41 | + { |
| 42 | + return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); |
| 43 | + } |
| 44 | + public static double DistanceD(PointF p1, PointF p2) |
| 45 | + { |
| 46 | + return DistanceD(p1.X, p1.Y, p2.X, p2.Y); |
| 47 | + } |
| 48 | + public static float DistanceF(float value1, float value2) |
| 49 | + { |
| 50 | + return Math.Abs(value1 - value2); |
| 51 | + } |
| 52 | + public static float DistanceF(float x1, float y1, float x2, float y2) |
| 53 | + { |
| 54 | + return (float)DistanceD(x1, y1, x2, y2); |
| 55 | + } |
| 56 | + public static float DistanceF(PointF p1, PointF p2) |
| 57 | + { |
| 58 | + return (float)DistanceD(p1, p2); |
| 59 | + } |
| 60 | + /// <summary> |
| 61 | + /// Linear interpolation between two values. |
| 62 | + /// </summary> |
| 63 | + /// <param name="from_value"></param> |
| 64 | + /// <param name="to_value"></param> |
| 65 | + /// <param name="speed"></param> |
| 66 | + /// <returns></returns> |
| 67 | + public static float FloatLerp(float from_value, float to_value, float speed) |
| 68 | + { |
| 69 | + return from_value + (to_value - from_value) * speed * UnityEngine.Time.deltaTime; |
| 70 | + } |
| 71 | + public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount) |
| 72 | + { |
| 73 | + // originaly from https://github.com/mono/MonoGame/ |
| 74 | + // All transformed to double not to lose precission |
| 75 | + // Otherwise, for high numbers of param:amount the result is NaN instead of Infinity |
| 76 | + double v1 = value1, v2 = value2, t1 = tangent1, t2 = tangent2, s = amount, result; |
| 77 | + double sCubed = s * s * s; |
| 78 | + double sSquared = s * s; |
| 79 | + |
| 80 | + if (amount == 0f) |
| 81 | + result = value1; |
| 82 | + else if (amount == 1f) |
| 83 | + result = value2; |
| 84 | + else |
| 85 | + result = (2 * v1 - 2 * v2 + t2 + t1) * sCubed + |
| 86 | + (3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared + |
| 87 | + t1 * s + |
| 88 | + v1; |
| 89 | + return (float)result; |
| 90 | + } |
| 91 | + /// <summary> |
| 92 | + /// Interpolates between two values using a cubic equation. |
| 93 | + /// </summary> |
| 94 | + /// <param name="value1">Source value.</param> |
| 95 | + /// <param name="value2">Source value.</param> |
| 96 | + /// <param name="amount">Weighting value.</param> |
| 97 | + /// <returns>Interpolated value.</returns> |
| 98 | + public static float SmoothStep(float value1, float value2, float amount) |
| 99 | + { |
| 100 | + // originaly from https://github.com/mono/MonoGame/ |
| 101 | + // It is expected that 0 < amount < 1 |
| 102 | + // If amount < 0, return value1 |
| 103 | + // If amount > 1, return value2 |
| 104 | + float result = MathHelper.Clamp(amount * UnityEngine.Time.deltaTime, 0f, 1f); |
| 105 | + result = MathHelper.Hermite(value1, 0f, value2, 0f, result); |
| 106 | + |
| 107 | + return result; |
| 108 | + } |
| 109 | + public static float Step(float from_value, float to_value, float speed) |
| 110 | + { |
| 111 | + float uSpeed = speed * UnityEngine.Time.deltaTime; |
| 112 | + if (Math.Abs(from_value - to_value) < uSpeed) return to_value; |
| 113 | + |
| 114 | + if (from_value < to_value) |
| 115 | + return from_value + uSpeed; |
| 116 | + else |
| 117 | + return from_value - uSpeed; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments