Math.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Copyright (c) 2013 Jason Bell
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. // DEALINGS IN THE SOFTWARE.
  21. //
  22. using System;
  23. namespace LibNoise
  24. {
  25. /// <summary>
  26. /// Provides math operations not found in System.Math.
  27. /// </summary>
  28. public class Math
  29. {
  30. /// <summary>
  31. /// Returns the given value clamped between the given lower and upper bounds.
  32. /// </summary>
  33. public static int ClampValue(int value, int lowerBound, int upperBound)
  34. {
  35. if (value < lowerBound)
  36. {
  37. return lowerBound;
  38. }
  39. else if (value > upperBound)
  40. {
  41. return upperBound;
  42. }
  43. else
  44. {
  45. return value;
  46. }
  47. }
  48. /// <summary>
  49. /// Returns the cubic interpolation of two values bound between two other values.
  50. /// </summary>
  51. /// <param name="n0">The value before the first value.</param>
  52. /// <param name="n1">The first value.</param>
  53. /// <param name="n2">The second value.</param>
  54. /// <param name="n3">The value after the second value.</param>
  55. /// <param name="a">The alpha value.</param>
  56. /// <returns></returns>
  57. protected double CubicInterpolate(double n0, double n1, double n2, double n3, double a)
  58. {
  59. double p = (n3 - n2) - (n0 - n1);
  60. double q = (n0 - n1) - p;
  61. double r = n2 - n0;
  62. double s = n1;
  63. return p * a * a * a + q * a * a + r * a + s;
  64. }
  65. /// <summary>
  66. /// Returns the smaller of the two given numbers.
  67. /// </summary>
  68. public static double GetSmaller(double a, double b)
  69. {
  70. return (a < b ? a : b);
  71. }
  72. /// <summary>
  73. /// Returns the larger of the two given numbers.
  74. /// </summary>
  75. public static double GetLarger(double a, double b)
  76. {
  77. return (a > b ? a : b);
  78. }
  79. /// <summary>
  80. /// Swaps the values contained by the two given variables.
  81. /// </summary>
  82. public static void SwapValues(ref double a, ref double b)
  83. {
  84. double c = a;
  85. a = b;
  86. b = c;
  87. }
  88. /// <summary>
  89. /// Returns the linear interpolation of two values with the given alpha.
  90. /// </summary>
  91. protected double LinearInterpolate(double n0, double n1, double a)
  92. {
  93. return ((1.0 - a) * n0) + (a * n1);
  94. }
  95. /// <summary>
  96. /// Returns the given value, modified to be able to fit into a 32-bit integer.
  97. /// </summary>
  98. /*public double MakeInt32Range(double n)
  99. {
  100. if (n >= 1073741824.0)
  101. {
  102. return ((2.0 * System.Math.IEEERemainder(n, 1073741824.0)) - 1073741824.0);
  103. }
  104. else if (n <= -1073741824.0)
  105. {
  106. return ((2.0 * System.Math.IEEERemainder(n, 1073741824.0)) + 1073741824.0);
  107. }
  108. else
  109. {
  110. return n;
  111. }
  112. }*/
  113. /// <summary>
  114. /// Returns the given value mapped onto a cubic S-curve.
  115. /// </summary>
  116. protected double SCurve3(double a)
  117. {
  118. return (a * a * (3.0 - 2.0 * a));
  119. }
  120. /// <summary>
  121. /// Returns the given value mapped onto a quintic S-curve.
  122. /// </summary>
  123. protected double SCurve5(double a)
  124. {
  125. double a3 = a * a * a;
  126. double a4 = a3 * a;
  127. double a5 = a4 * a;
  128. return (6.0 * a5) - (15.0 * a4) + (10.0 * a3);
  129. }
  130. /// <summary>
  131. /// Returns the value of the mathematical constant PI.
  132. /// </summary>
  133. public static readonly double PI = 3.1415926535897932385;
  134. /// <summary>
  135. /// Returns the square root of 2.
  136. /// </summary>
  137. public static readonly double Sqrt2 = 1.4142135623730950488;
  138. /// <summary>
  139. /// Returns the square root of 3.
  140. /// </summary>
  141. public static readonly double Sqrt3 = 1.7320508075688772935;
  142. /// <summary>
  143. /// Returns PI/180.0, used for converting degrees to radians.
  144. /// </summary>
  145. public static readonly double DEG_TO_RAD = PI / 180.0;
  146. /// <summary>
  147. /// Provides the X, Y, and Z coordinates on the surface of a sphere
  148. /// cooresponding to the given latitude and longitude.
  149. /// </summary>
  150. protected void LatLonToXYZ(double lat, double lon, ref double x, ref double y, ref double z)
  151. {
  152. double r = System.Math.Cos (DEG_TO_RAD * lat);
  153. x = r * System.Math.Cos(DEG_TO_RAD * lon);
  154. y = System.Math.Sin(DEG_TO_RAD * lat);
  155. z = r * System.Math.Sin(DEG_TO_RAD * lon);
  156. }
  157. }
  158. }