MathEx.cs 5.3 KB

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