Unity 使用this关键字进行函数拓展 - Math

举报
CoderZ1010 发表于 2022/09/25 06:51:07 2022/09/25
【摘要】 using UnityEngine;using Random = UnityEngine.Random; using System;using System.Collections.Generic; namespace SK.Framework{ /// <summary> /// 算术相关拓展 /// &...

  
  1. using UnityEngine;
  2. using Random = UnityEngine.Random;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SK.Framework
  6. {
  7. /// <summary>
  8. /// 算术相关拓展
  9. /// </summary>
  10. public static class MathExtension
  11. {
  12. /// <summary>
  13. /// 保留小数指定位数
  14. /// </summary>
  15. /// <param name="self">float值</param>
  16. /// <param name="point">保留位置</param>
  17. /// <returns>保留指定小数位数后的float值</returns>
  18. public static float Round(this float self, int point)
  19. {
  20. int scale = 1;
  21. for (int i = 0; i < point; i++)
  22. {
  23. scale *= 10;
  24. }
  25. self *= scale;
  26. return Mathf.Round(self) / scale;
  27. }
  28. /// <summary>
  29. /// 判断是否约等于目标值
  30. /// </summary>
  31. /// <param name="self">float值</param>
  32. /// <param name="targetValue">目标值</param>
  33. /// <returns>若约等于则返回true,否则返回false</returns>
  34. public static bool IsApproximately(this float self, float targetValue)
  35. {
  36. return Mathf.Approximately(self, targetValue);
  37. }
  38. /// <summary>
  39. /// 阶乘
  40. /// </summary>
  41. /// <param name="self">int值</param>
  42. /// <returns>阶乘结果</returns>
  43. public static int Fact(this int self)
  44. {
  45. if (self == 0)
  46. {
  47. return 1;
  48. }
  49. else
  50. {
  51. return self * Fact(self - 1);
  52. }
  53. }
  54. /// <summary>
  55. /// 平方和根
  56. /// </summary>
  57. /// <param name="x">int值</param>
  58. /// <param name="y">int值</param>
  59. /// <returns>x与y的平方和根</returns>
  60. public static float Sqrt(this int x, int y)
  61. {
  62. int n2 = x ^ 2 + y ^ 2;
  63. return Mathf.Sqrt(n2);
  64. }
  65. /// <summary>
  66. /// 获取随机元素
  67. /// </summary>
  68. /// <typeparam name="T">元素类型</typeparam>
  69. /// <param name="self">数组</param>
  70. /// <returns>随机值</returns>
  71. public static T GetRandomValue<T>(this T[] self)
  72. {
  73. return self[Random.Range(0, self.Length)];
  74. }
  75. /// <summary>
  76. /// 获取随机元素
  77. /// </summary>
  78. /// <typeparam name="T">元素类型</typeparam>
  79. /// <param name="self">列表</param>
  80. /// <returns>随机值</returns>
  81. public static T GetRandomValue<T>(this List<T> self)
  82. {
  83. return self[Random.Range(0, self.Count)];
  84. }
  85. /// <summary>
  86. /// 获取指定个数随机元素
  87. /// </summary>
  88. /// <typeparam name="T">元素类型</typeparam>
  89. /// <param name="self">数组</param>
  90. /// <param name="count">个数</param>
  91. /// <returns>元素数组</returns>
  92. public static T[] GetRandomValue<T>(this T[] self, int count)
  93. {
  94. if (count > self.Length)
  95. {
  96. throw new ArgumentOutOfRangeException();
  97. }
  98. List<T> tempList = new List<T>(self.Length);
  99. for (int i = 0; i < self.Length; i++)
  100. {
  101. tempList.Add(self[i]);
  102. }
  103. T[] retArray = new T[count];
  104. for (int i = 0; i < retArray.Length; i++)
  105. {
  106. int index = Random.Range(0, tempList.Count);
  107. retArray[i] = tempList[index];
  108. tempList.RemoveAt(index);
  109. }
  110. return retArray;
  111. }
  112. /// <summary>
  113. /// 获取指定个数随机元素
  114. /// </summary>
  115. /// <typeparam name="T">元素类型</typeparam>
  116. /// <param name="self">列表</param>
  117. /// <param name="count">个数</param>
  118. /// <returns>元素数组</returns>
  119. public static T[] GetRandomValue<T>(this List<T> self, int count)
  120. {
  121. if (count > self.Count)
  122. {
  123. throw new ArgumentOutOfRangeException();
  124. }
  125. List<T> tempList = new List<T>(self.Count);
  126. for (int i = 0; i < self.Count; i++)
  127. {
  128. tempList.Add(self[i]);
  129. }
  130. T[] retArray = new T[count];
  131. for (int i = 0; i < retArray.Length; i++)
  132. {
  133. int index = Random.Range(0, tempList.Count);
  134. retArray[i] = tempList[index];
  135. tempList.RemoveAt(index);
  136. }
  137. return retArray;
  138. }
  139. /// <summary>
  140. /// 计算多边形周长
  141. /// </summary>
  142. /// <param name="self">多边形顶点数组</param>
  143. /// <returns>周长</returns>
  144. public static float GetPolygonPerimeter(this Vector3[] self)
  145. {
  146. if (self.Length < 3) return 0.0f;
  147. float retV = 0f;
  148. for (int i = 0; i < self.Length; i++)
  149. {
  150. retV += Vector3.Distance(self[i], self[(i + 1 < self.Length ? i + 1 : 0)]);
  151. }
  152. return retV;
  153. }
  154. /// <summary>
  155. /// 计算多边形周长
  156. /// </summary>
  157. /// <param name="self">多边形顶点列表</param>
  158. /// <returns>周长</returns>
  159. public static float GetPolygonPerimeter(this List<Vector3> self)
  160. {
  161. if (self.Count < 3) return 0.0f;
  162. float retV = 0f;
  163. for (int i = 0; i < self.Count; i++)
  164. {
  165. retV += Vector3.Distance(self[i], self[(i + 1 < self.Count ? i + 1 : 0)]);
  166. }
  167. return retV;
  168. }
  169. /// <summary>
  170. /// 计算多边形面积
  171. /// </summary>
  172. /// <param name="self">多边形顶点数组</param>
  173. /// <returns>面积</returns>
  174. public static float GetPolygonArea(this Vector3[] self)
  175. {
  176. if (self.Length < 3) return 0.0f;
  177. float retV = self[0].z * (self[self.Length - 1].x - self[1].x);
  178. for (int i = 1; i < self.Length; i++)
  179. {
  180. retV += self[i].z * (self[i - 1].x - self[(i + 1) % self.Length].x);
  181. }
  182. return Mathf.Abs(retV / 2.0f);
  183. }
  184. /// <summary>
  185. /// 计算多边形面积
  186. /// </summary>
  187. /// <param name="self">多边形顶点列表</param>
  188. /// <returns>面积</returns>
  189. public static float GetPolygonArea(this List<Vector3> self)
  190. {
  191. if (self.Count < 3) return 0.0f;
  192. float retV = self[0].z * (self[self.Count - 1].x - self[1].x);
  193. for (int i = 1; i < self.Count; i++)
  194. {
  195. retV += self[i].z * (self[i - 1].x - self[(i + 1) % self.Count].x);
  196. }
  197. return Mathf.Abs(retV / 2.0f);
  198. }
  199. /// <summary>
  200. /// 计算圆的周长
  201. /// </summary>
  202. /// <param name="self">半径</param>
  203. /// <returns>周长</returns>
  204. public static float GetCirclePerimeter(this float self)
  205. {
  206. return Mathf.PI * 2f * self;
  207. }
  208. /// <summary>
  209. /// 计算圆的面积
  210. /// </summary>
  211. /// <param name="self">半径</param>
  212. /// <returns>面积</returns>
  213. public static float GetCircleArea(this float self)
  214. {
  215. return Mathf.PI * Mathf.Pow(self, 2);
  216. }
  217. /// <summary>
  218. /// 三角函数计算对边的长度
  219. /// </summary>
  220. /// <param name="self">角度</param>
  221. /// <param name="neighbouringSideLength">邻边的长度</param>
  222. /// <returns>对边的长度</returns>
  223. public static float GetFaceSideLength(this float self, float neighbouringSideLength)
  224. {
  225. return neighbouringSideLength * Mathf.Tan(self * Mathf.Deg2Rad);
  226. }
  227. /// <summary>
  228. /// 三角函数计算邻边的长度
  229. /// </summary>
  230. /// <param name="self">角度</param>
  231. /// <param name="faceSideLength">对边的长度</param>
  232. /// <returns>邻边的长度</returns>
  233. public static float GetNeighbouringSideLength(this float self, float faceSideLength)
  234. {
  235. return faceSideLength / Mathf.Tan(self * Mathf.Deg2Rad);
  236. }
  237. /// <summary>
  238. /// 勾股定理计算斜边的长度
  239. /// </summary>
  240. /// <param name="self">直角边的长度</param>
  241. /// <param name="anotherRightangleSideLength">另一条直角边的长度</param>
  242. /// <returns>斜边的长度</returns>
  243. public static float GetHypotenuseLength(this float self, float anotherRightangleSideLength)
  244. {
  245. return Mathf.Sqrt(Mathf.Pow(self, 2f) + Mathf.Pow(anotherRightangleSideLength, 2f));
  246. }
  247. /// <summary>
  248. /// 正弦
  249. /// </summary>
  250. /// <param name="self">角度</param>
  251. /// <returns>正弦值</returns>
  252. public static float Sin(this float self)
  253. {
  254. return Mathf.Sin(self * Mathf.Deg2Rad);
  255. }
  256. /// <summary>
  257. /// 余弦
  258. /// </summary>
  259. /// <param name="self">角度</param>
  260. /// <returns></returns>
  261. public static float Cos(this float self)
  262. {
  263. return Mathf.Cos(self * Mathf.Deg2Rad);
  264. }
  265. /// <summary>
  266. /// 正切
  267. /// </summary>
  268. /// <param name="self">角度</param>
  269. /// <returns>正切值</returns>
  270. public static float Tan(this float self)
  271. {
  272. return Mathf.Tan(self * Mathf.Deg2Rad);
  273. }
  274. /// <summary>
  275. /// 反正弦
  276. /// </summary>
  277. /// <param name="self">正弦值</param>
  278. /// <returns>角度</returns>
  279. public static float ArcSin(this float self)
  280. {
  281. return Mathf.Asin(self) * Mathf.Rad2Deg;
  282. }
  283. /// <summary>
  284. /// 反余弦
  285. /// </summary>
  286. /// <param name="self">余弦值</param>
  287. /// <returns>角度</returns>
  288. public static float ArcCos(this float self)
  289. {
  290. return Mathf.Acos(self) * Mathf.Rad2Deg;
  291. }
  292. /// <summary>
  293. /// 反正切
  294. /// </summary>
  295. /// <param name="self">正切值</param>
  296. /// <returns>角度</returns>
  297. public static float ArcTan(this float self)
  298. {
  299. return Mathf.Atan(self) * Mathf.Rad2Deg;
  300. }
  301. /// <summary>
  302. /// 度转弧度
  303. /// </summary>
  304. /// <param name="self"></param>
  305. /// <returns>弧度</returns>
  306. public static float Deg2Rad(this float self)
  307. {
  308. return self * Mathf.Deg2Rad;
  309. }
  310. /// <summary>
  311. /// 弧度转度
  312. /// </summary>
  313. /// <param name="self">弧度</param>
  314. /// <returns></returns>
  315. public static float Rad2Deg(this float self)
  316. {
  317. return self * Mathf.Rad2Deg;
  318. }
  319. }
  320. }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/116752656

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。