MathEx.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. namespace Microsoft
  3. {
  4. /// <summary>
  5. /// Math类扩展
  6. /// Copyright (c) JajaSoft
  7. /// </summary>
  8. public static class MathEx
  9. {
  10. #region 字段
  11. /// <summary>
  12. /// E
  13. /// </summary>
  14. public const float E = 2.718282f;
  15. /// <summary>
  16. /// Log10E
  17. /// </summary>
  18. public const float Log10E = 0.4342945f;
  19. /// <summary>
  20. /// Log2E
  21. /// </summary>
  22. public const float Log2E = 1.442695f;
  23. /// <summary>
  24. /// Pi
  25. /// </summary>
  26. public const float Pi = 3.141593f;
  27. /// <summary>
  28. /// Pi/2
  29. /// </summary>
  30. public const float PiOver2 = 1.570796f;
  31. /// <summary>
  32. /// Pi/4
  33. /// </summary>
  34. public const float PiOver4 = 0.7853982f;
  35. /// <summary>
  36. /// Pi*2
  37. /// </summary>
  38. public const float TwoPi = 6.283185f;
  39. /// <summary>
  40. /// Double类型的 180d / Math.PI
  41. /// </summary>
  42. public const double Double180OverPi = 180d / Math.PI;
  43. /// <summary>
  44. /// Double类型的 Math.PI / 180d
  45. /// </summary>
  46. public const double DoublePiOver180 = Math.PI / 180d;
  47. /// <summary>
  48. /// Single类型的 180d / Math.PI
  49. /// </summary>
  50. public const float Single180OverPi = (float)Double180OverPi;
  51. /// <summary>
  52. /// Single类型的 Math.PI / 180d
  53. /// </summary>
  54. public const float SinglePiOver180 = (float)DoublePiOver180;
  55. #endregion
  56. #region 方法
  57. /// <summary>
  58. /// 返回指定范围内的限定值
  59. /// </summary>
  60. /// <param name="value">值</param>
  61. /// <param name="min">最小值</param>
  62. /// <param name="max">最大值</param>
  63. /// <returns>限定值</returns>
  64. public static Byte Clamp(Byte value, Byte min, Byte max)
  65. {
  66. value = (value > max) ? max : value;
  67. value = (value < min) ? min : value;
  68. return value;
  69. }
  70. /// <summary>
  71. /// 返回指定范围内的限定值
  72. /// </summary>
  73. /// <param name="value">值</param>
  74. /// <param name="min">最小值</param>
  75. /// <param name="max">最大值</param>
  76. /// <returns>限定值</returns>
  77. public static Decimal Clamp(Decimal value, Decimal min, Decimal max)
  78. {
  79. value = (value > max) ? max : value;
  80. value = (value < min) ? min : value;
  81. return value;
  82. }
  83. /// <summary>
  84. /// 返回指定范围内的限定值
  85. /// </summary>
  86. /// <param name="value">值</param>
  87. /// <param name="min">最小值</param>
  88. /// <param name="max">最大值</param>
  89. /// <returns>限定值</returns>
  90. public static Double Clamp(Double value, Double min, Double max)
  91. {
  92. value = (value > max) ? max : value;
  93. value = (value < min) ? min : value;
  94. return value;
  95. }
  96. /// <summary>
  97. /// 返回指定范围内的限定值
  98. /// </summary>
  99. /// <param name="value">值</param>
  100. /// <param name="min">最小值</param>
  101. /// <param name="max">最大值</param>
  102. /// <returns>限定值</returns>
  103. public static Int16 Clamp(Int16 value, Int16 min, Int16 max)
  104. {
  105. value = (value > max) ? max : value;
  106. value = (value < min) ? min : value;
  107. return value;
  108. }
  109. /// <summary>
  110. /// 返回指定范围内的限定值
  111. /// </summary>
  112. /// <param name="value">值</param>
  113. /// <param name="min">最小值</param>
  114. /// <param name="max">最大值</param>
  115. /// <returns>限定值</returns>
  116. public static Int32 Clamp(Int32 value, Int32 min, Int32 max)
  117. {
  118. value = (value > max) ? max : value;
  119. value = (value < min) ? min : value;
  120. return value;
  121. }
  122. /// <summary>
  123. /// 返回指定范围内的限定值
  124. /// </summary>
  125. /// <param name="value">值</param>
  126. /// <param name="min">最小值</param>
  127. /// <param name="max">最大值</param>
  128. /// <returns>限定值</returns>
  129. public static Int64 Clamp(Int64 value, Int64 min, Int64 max)
  130. {
  131. value = (value > max) ? max : value;
  132. value = (value < min) ? min : value;
  133. return value;
  134. }
  135. /// <summary>
  136. /// 返回指定范围内的限定值
  137. /// </summary>
  138. /// <param name="value">值</param>
  139. /// <param name="min">最小值</param>
  140. /// <param name="max">最大值</param>
  141. /// <returns>限定值</returns>
  142. public static SByte Clamp(SByte value, SByte min, SByte max)
  143. {
  144. value = (value > max) ? max : value;
  145. value = (value < min) ? min : value;
  146. return value;
  147. }
  148. /// <summary>
  149. /// 返回指定范围内的限定值
  150. /// </summary>
  151. /// <param name="value">值</param>
  152. /// <param name="min">最小值</param>
  153. /// <param name="max">最大值</param>
  154. /// <returns>限定值</returns>
  155. public static Single Clamp(Single value, Single min, Single max)
  156. {
  157. value = (value > max) ? max : value;
  158. value = (value < min) ? min : value;
  159. return value;
  160. }
  161. /// <summary>
  162. /// 返回指定范围内的限定值
  163. /// </summary>
  164. /// <param name="value">值</param>
  165. /// <param name="min">最小值</param>
  166. /// <param name="max">最大值</param>
  167. /// <returns>限定值</returns>
  168. public static UInt16 Clamp(UInt16 value, UInt16 min, UInt16 max)
  169. {
  170. value = (value > max) ? max : value;
  171. value = (value < min) ? min : value;
  172. return value;
  173. }
  174. /// <summary>
  175. /// 返回指定范围内的限定值
  176. /// </summary>
  177. /// <param name="value">值</param>
  178. /// <param name="min">最小值</param>
  179. /// <param name="max">最大值</param>
  180. /// <returns>限定值</returns>
  181. public static UInt32 Clamp(UInt32 value, UInt32 min, UInt32 max)
  182. {
  183. value = (value > max) ? max : value;
  184. value = (value < min) ? min : value;
  185. return value;
  186. }
  187. /// <summary>
  188. /// 返回指定范围内的限定值
  189. /// </summary>
  190. /// <param name="value">值</param>
  191. /// <param name="min">最小值</param>
  192. /// <param name="max">最大值</param>
  193. /// <returns>限定值</returns>
  194. public static UInt64 Clamp(UInt64 value, UInt64 min, UInt64 max)
  195. {
  196. value = (value > max) ? max : value;
  197. value = (value < min) ? min : value;
  198. return value;
  199. }
  200. /// <summary>
  201. /// 弧度转换为角度
  202. /// </summary>
  203. /// <param name="radians">弧度</param>
  204. /// <returns>角度</returns>
  205. public static Double ToDegrees(Double radians)
  206. {
  207. return (radians * Double180OverPi);
  208. }
  209. /// <summary>
  210. /// 弧度转换为角度
  211. /// </summary>
  212. /// <param name="radians">弧度</param>
  213. /// <returns>角度</returns>
  214. public static Single ToDegrees(Single radians)
  215. {
  216. return (radians * Single180OverPi);
  217. }
  218. /// <summary>
  219. /// 角度转换为弧度
  220. /// </summary>
  221. /// <param name="degrees">角度</param>
  222. /// <returns>弧度</returns>
  223. public static Double ToRadians(Double degrees)
  224. {
  225. return (degrees * DoublePiOver180);
  226. }
  227. /// <summary>
  228. /// 角度转换为弧度
  229. /// </summary>
  230. /// <param name="degrees">角度</param>
  231. /// <returns>弧度</returns>
  232. public static Single ToRadians(Single degrees)
  233. {
  234. return (degrees * SinglePiOver180);
  235. }
  236. #endregion
  237. }
  238. }