2
0

MathEx.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. namespace BansheeEngine
  3. {
  4. public enum EulerAngleOrder
  5. {
  6. XYZ,
  7. XZY,
  8. YXZ,
  9. YZX,
  10. ZXY,
  11. ZYX
  12. };
  13. class MathEx
  14. {
  15. public const float Pi = 3.141593f;
  16. public const float TwoPi = (2.0f * Pi);
  17. public const float HalfPi = (0.5f * Pi);
  18. public const float Deg2Rad = Pi / 180.0f;
  19. public const float Rad2Deg = 180.0f / Pi;
  20. public static float Min(float a, float b)
  21. {
  22. if (a < b)
  23. return a;
  24. return b;
  25. }
  26. public static float Min(params float[] values)
  27. {
  28. int length = values.Length;
  29. if (length == 0)
  30. return 0.0f;
  31. float min = values[0];
  32. for (int i = 1; i < length; i++)
  33. {
  34. if (values[i] < min)
  35. min = values[i];
  36. }
  37. return min;
  38. }
  39. public static int Min(int a, int b)
  40. {
  41. if (a < b)
  42. return a;
  43. return b;
  44. }
  45. public static int Min(params int[] values)
  46. {
  47. int length = values.Length;
  48. if (length == 0)
  49. return 0;
  50. int min = values[0];
  51. for (int i = 1; i < length; i++)
  52. {
  53. if (values[i] < min)
  54. min = values[i];
  55. }
  56. return min;
  57. }
  58. public static float Max(float a, float b)
  59. {
  60. if (a > b)
  61. return a;
  62. return b;
  63. }
  64. public static float Max(params float[] values)
  65. {
  66. int length = values.Length;
  67. if (length == 0)
  68. return 0.0f;
  69. float max = values[0];
  70. for (int i = 1; i < length; i++)
  71. {
  72. if (values[i] > max)
  73. max = values[i];
  74. }
  75. return max;
  76. }
  77. public static int Max(int a, int b)
  78. {
  79. if (a > b)
  80. return a;
  81. else
  82. return b;
  83. }
  84. public static int Max(params int[] values)
  85. {
  86. int length = values.Length;
  87. if (length == 0)
  88. return 0;
  89. int max = values[0];
  90. for (int i = 1; i < length; ++i)
  91. {
  92. if (values[i] > max)
  93. max = values[i];
  94. }
  95. return max;
  96. }
  97. public static float Abs(float f)
  98. {
  99. return Math.Abs(f);
  100. }
  101. public static int Abs(int value)
  102. {
  103. return Math.Abs(value);
  104. }
  105. public static float Pow(float f, float p)
  106. {
  107. return (float)Math.Pow(f, p);
  108. }
  109. public static float Exp(float power)
  110. {
  111. return (float)Math.Exp(power);
  112. }
  113. public static float Log(float f, float p)
  114. {
  115. return (float)Math.Log(f, p);
  116. }
  117. public static float Log(float f)
  118. {
  119. return (float)Math.Log(f);
  120. }
  121. public static float Log10(float f)
  122. {
  123. return (float)Math.Log10(f);
  124. }
  125. public static float Ceil(float f)
  126. {
  127. return (float)Math.Ceiling(f);
  128. }
  129. public static float Floor(float f)
  130. {
  131. return (float)Math.Floor(f);
  132. }
  133. public static float Round(float f)
  134. {
  135. return (float)Math.Round(f);
  136. }
  137. public static int CeilToInt(float f)
  138. {
  139. return (int)Math.Ceiling(f);
  140. }
  141. public static int FloorToInt(float f)
  142. {
  143. return (int)Math.Floor(f);
  144. }
  145. public static int RoundToInt(float f)
  146. {
  147. return (int)Math.Round(f);
  148. }
  149. public static float Sign(float f)
  150. {
  151. return f >= 0.0f ? 1.0f : -1.0f;
  152. }
  153. public static float Sin(float f)
  154. {
  155. return (float)Math.Sin(f);
  156. }
  157. public static float Cos(float f)
  158. {
  159. return (float)Math.Cos(f);
  160. }
  161. public static float Tan(float f)
  162. {
  163. return (float)Math.Tan(f);
  164. }
  165. public static float Asin(float f)
  166. {
  167. return (float)Math.Asin(f);
  168. }
  169. public static float Acos(float f)
  170. {
  171. return (float)Math.Acos(f);
  172. }
  173. public static float Atan(float f)
  174. {
  175. return (float)Math.Atan(f);
  176. }
  177. public static float Atan2(float y, float x)
  178. {
  179. return (float)Math.Atan2(y, x);
  180. }
  181. public static float Sqrt(float f)
  182. {
  183. return (float)Math.Sqrt(f);
  184. }
  185. public static float InvSqrt(float f)
  186. {
  187. return 1.0f/(float) Math.Sqrt(f);
  188. }
  189. public static float Clamp(float value, float min, float max)
  190. {
  191. if (value < min)
  192. value = min;
  193. else if (value > max)
  194. value = max;
  195. return value;
  196. }
  197. public static int Clamp(int value, int min, int max)
  198. {
  199. if (value < min)
  200. value = min;
  201. else if (value > max)
  202. value = max;
  203. return value;
  204. }
  205. public static float Clamp01(float value)
  206. {
  207. if (value < 0.0)
  208. return 0.0f;
  209. if (value > 1.0)
  210. return 1f;
  211. return value;
  212. }
  213. public static Degree WrapAngle(Degree angle)
  214. {
  215. const float inv360 = 1.0f/360.0f;
  216. float angleVal = angle.Degrees;
  217. float wrapCount = (float)MathEx.Floor(MathEx.Abs(angleVal * inv360));
  218. if (angleVal > 0.0f)
  219. angleVal -= 360.0f * wrapCount;
  220. else
  221. angleVal += 360.0f * wrapCount;
  222. return new Degree(angleVal);
  223. }
  224. }
  225. }