/Main/src/DynamicDataDisplay/Common/Auxiliary/MathHelper.cs
C# | 91 lines | 68 code | 15 blank | 8 comment | 1 complexity | 0a9bfce019e6dc222f5ca8b79c1134c9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System; 2using System.Windows; 3 4namespace Microsoft.Research.DynamicDataDisplay 5{ 6 public static class MathHelper 7 { 8 public static long Clamp(long value, long min, long max) 9 { 10 return Math.Max(min, Math.Min(value, max)); 11 } 12 13 public static double Clamp(double value, double min, double max) 14 { 15 return Math.Max(min, Math.Min(value, max)); 16 } 17 18 /// <summary>Clamps specified value to [0,1]</summary> 19 /// <param name="d">Value to clamp</param> 20 /// <returns>Value in range [0,1]</returns> 21 public static double Clamp(double value) 22 { 23 return Math.Max(0, Math.Min(value, 1)); 24 } 25 26 public static int Clamp(int value, int min, int max) 27 { 28 return Math.Max(min, Math.Min(value, max)); 29 } 30 31 public static Rect CreateRectByPoints(double xMin, double yMin, double xMax, double yMax) 32 { 33 return new Rect(new Point(xMin, yMin), new Point(xMax, yMax)); 34 } 35 36 public static double Interpolate(double start, double end, double ratio) 37 { 38 return start * (1 - ratio) + end * ratio; 39 } 40 41 public static double RadiansToDegrees(this double radians) 42 { 43 return radians * 180 / Math.PI; 44 } 45 46 public static double DegreesToRadians(this double degrees) 47 { 48 return degrees / 180 * Math.PI; 49 } 50 51 /// <summary> 52 /// Converts vector into angle. 53 /// </summary> 54 /// <param name="vector">The vector.</param> 55 /// <returns>Angle in degrees.</returns> 56 public static double ToAngle(this Vector vector) 57 { 58 return Math.Atan2(-vector.Y, vector.X).RadiansToDegrees(); 59 } 60 61 public static Point ToPoint(this Vector v) 62 { 63 return new Point(v.X, v.Y); 64 } 65 66 public static bool IsNaN(this double d) 67 { 68 return Double.IsNaN(d); 69 } 70 71 public static bool IsNotNaN(this double d) 72 { 73 return !Double.IsNaN(d); 74 } 75 76 public static bool IsFinite(this double d) 77 { 78 return !Double.IsNaN(d) && !Double.IsInfinity(d); 79 } 80 81 public static bool IsInfinite(this double d) 82 { 83 return Double.IsInfinity(d); 84 } 85 86 public static bool AreClose(double d1, double d2, double diffRatio) 87 { 88 return Math.Abs(d1 / d2 - 1) < diffRatio; 89 } 90 } 91}