1 | using System;
|
---|
2 | using System.Windows;
|
---|
3 |
|
---|
4 | namespace 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 | }
|
---|