PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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