| 1 | using System; | 
 
 
 
 
 | 2 |  | 
 
 
 
 
 | 3 | namespace Oni | 
 
 
 
 
 | 4 | { | 
 
 
 
 
 | 5 | internal static class MathHelper | 
 
 
 
 
 | 6 | { | 
 
 
 
 
 | 7 | public const float Eps = 1e-5f; | 
 
 
 
 
 | 8 | public const float Pi = 3.141593f; | 
 
 
 
 
 | 9 | public const float HalfPi = Pi / 2.0f; | 
 
 
 
 
 | 10 | public const float PiOver4 = Pi / 4.0f; | 
 
 
 
 
 | 11 | public const float TwoPi = 2.0f * Pi; | 
 
 
 
 
 | 12 |  | 
 
 
 
 
 | 13 | public static float ToDegrees(float radians) => radians * (180.0f / Pi); | 
 
 
 
 
 | 14 | public static float ToRadians(float degrees) => degrees * (Pi / 180.0f); | 
 
 
 
 
 | 15 | public static float Distance(float v1, float v2) => Math.Abs(v2 - v1); | 
 
 
 
 
 | 16 | public static float Lerp(float v1, float v2, float amount) => v1 + (v2 - v1) * amount; | 
 
 
 
 
 | 17 |  | 
 
 
 
 
 | 18 | public static int Lerp(int v1, int v2, float amount) | 
 
 
 
 
 | 19 | { | 
 
 
 
 
 | 20 | if (amount == 0.0f) | 
 
 
 
 
 | 21 | return v1; | 
 
 
 
 
 | 22 |  | 
 
 
 
 
 | 23 | if (amount == 1.0f) | 
 
 
 
 
 | 24 | return v2; | 
 
 
 
 
 | 25 |  | 
 
 
 
 
 | 26 | return (int)(v1 + (v2 - v1) * amount); | 
 
 
 
 
 | 27 | } | 
 
 
 
 
 | 28 |  | 
 
 
 
 
 | 29 | public static float Clamp(float v, float min, float max) | 
 
 
 
 
 | 30 | { | 
 
 
 
 
 | 31 | v = (v > max) ? max : v; | 
 
 
 
 
 | 32 | v = (v < min) ? min : v; | 
 
 
 
 
 | 33 |  | 
 
 
 
 
 | 34 | return v; | 
 
 
 
 
 | 35 | } | 
 
 
 
 
 | 36 |  | 
 
 
 
 
 | 37 | public static int Clamp(int v, int min, int max) | 
 
 
 
 
 | 38 | { | 
 
 
 
 
 | 39 | v = (v > max) ? max : v; | 
 
 
 
 
 | 40 | v = (v < min) ? min : v; | 
 
 
 
 
 | 41 |  | 
 
 
 
 
 | 42 | return v; | 
 
 
 
 
 | 43 | } | 
 
 
 
 
 | 44 |  | 
 
 
 
 
 | 45 | public static float Area(Vector2[] points) | 
 
 
 
 
 | 46 | { | 
 
 
 
 
 | 47 | float area = 0.0f; | 
 
 
 
 
 | 48 |  | 
 
 
 
 
 | 49 | for (int i = 0; i < points.Length; i++) | 
 
 
 
 
 | 50 | { | 
 
 
 
 
 | 51 | int j = (i + 1) % points.Length; | 
 
 
 
 
 | 52 | area += points[i].X * points[j].Y; | 
 
 
 
 
 | 53 | area -= points[i].Y * points[j].X; | 
 
 
 
 
 | 54 | } | 
 
 
 
 
 | 55 |  | 
 
 
 
 
 | 56 | return Math.Abs(area * 0.5f); | 
 
 
 
 
 | 57 | } | 
 
 
 
 
 | 58 | } | 
 
 
 
 
 | 59 | } |