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> /// 算术相关拓展 /// &...

      using UnityEngine;
      using Random = UnityEngine.Random;
      using System;
      using System.Collections.Generic;
      namespace SK.Framework
      {
         /// <summary>
         /// 算术相关拓展
         /// </summary>
         public static class MathExtension
          {
             /// <summary>
             /// 保留小数指定位数
             /// </summary>
             /// <param name="self">float值</param>
             /// <param name="point">保留位置</param>
             /// <returns>保留指定小数位数后的float值</returns>
             public static float Round(this float self, int point)
              {
                 int scale = 1;
                 for (int i = 0; i < point; i++)
                  {
                      scale *= 10;
                  }
                  self *= scale;
                 return Mathf.Round(self) / scale;
              }
             /// <summary>
             /// 判断是否约等于目标值
             /// </summary>
             /// <param name="self">float值</param>
             /// <param name="targetValue">目标值</param>
             /// <returns>若约等于则返回true,否则返回false</returns>
             public static bool IsApproximately(this float self, float targetValue)
              {
                 return Mathf.Approximately(self, targetValue);
              }
             /// <summary>
             /// 阶乘
             /// </summary>
             /// <param name="self">int值</param>
             /// <returns>阶乘结果</returns>
             public static int Fact(this int self)
              {
                 if (self == 0)
                  {
                     return 1;
                  }
                 else
                  {
                     return self * Fact(self - 1);
                  }
              }
             /// <summary>
             /// 平方和根
             /// </summary>
             /// <param name="x">int值</param>
             /// <param name="y">int值</param>
             /// <returns>x与y的平方和根</returns>
             public static float Sqrt(this int x, int y)
              {
                 int n2 = x ^ 2 + y ^ 2;
                 return Mathf.Sqrt(n2);
              }
             /// <summary>
             /// 获取随机元素
             /// </summary>
             /// <typeparam name="T">元素类型</typeparam>
             /// <param name="self">数组</param>
             /// <returns>随机值</returns>
             public static T GetRandomValue<T>(this T[] self)
              {
                 return self[Random.Range(0, self.Length)];
              }
             /// <summary>
             /// 获取随机元素
             /// </summary>
             /// <typeparam name="T">元素类型</typeparam>
             /// <param name="self">列表</param>
             /// <returns>随机值</returns>
             public static T GetRandomValue<T>(this List<T> self)
              {
                 return self[Random.Range(0, self.Count)];
              }
             /// <summary>
             /// 获取指定个数随机元素
             /// </summary>
             /// <typeparam name="T">元素类型</typeparam>
             /// <param name="self">数组</param>
             /// <param name="count">个数</param>
             /// <returns>元素数组</returns>
             public static T[] GetRandomValue<T>(this T[] self, int count)
              {
                 if (count > self.Length)
                  {
                     throw new ArgumentOutOfRangeException();
                  }
                  List<T> tempList = new List<T>(self.Length);
                 for (int i = 0; i < self.Length; i++)
                  {
                      tempList.Add(self[i]);
                  }
                  T[] retArray = new T[count];
                 for (int i = 0; i < retArray.Length; i++)
                  {
                     int index = Random.Range(0, tempList.Count);
                      retArray[i] = tempList[index];
                      tempList.RemoveAt(index);
                  }
                 return retArray;
              }
             /// <summary>
             /// 获取指定个数随机元素
             /// </summary>
             /// <typeparam name="T">元素类型</typeparam>
             /// <param name="self">列表</param>
             /// <param name="count">个数</param>
             /// <returns>元素数组</returns>
             public static T[] GetRandomValue<T>(this List<T> self, int count)
              {
                 if (count > self.Count)
                  {
                     throw new ArgumentOutOfRangeException();
                  }
                  List<T> tempList = new List<T>(self.Count);
                 for (int i = 0; i < self.Count; i++)
                  {
                      tempList.Add(self[i]);
                  }
                  T[] retArray = new T[count];
                 for (int i = 0; i < retArray.Length; i++)
                  {
                     int index = Random.Range(0, tempList.Count);
                      retArray[i] = tempList[index];
                      tempList.RemoveAt(index);
                  }
                 return retArray;
              }
             /// <summary>
             /// 计算多边形周长
             /// </summary>
             /// <param name="self">多边形顶点数组</param>
             /// <returns>周长</returns>
             public static float GetPolygonPerimeter(this Vector3[] self)
              {
                 if (self.Length < 3) return 0.0f;
                 float retV = 0f;
                 for (int i = 0; i < self.Length; i++)
                  {
                      retV += Vector3.Distance(self[i], self[(i + 1 < self.Length ? i + 1 : 0)]);
                  }
                 return retV;
              }
             /// <summary>
             /// 计算多边形周长
             /// </summary>
             /// <param name="self">多边形顶点列表</param>
             /// <returns>周长</returns>
             public static float GetPolygonPerimeter(this List<Vector3> self)
              {
                 if (self.Count < 3) return 0.0f;
                 float retV = 0f;
                 for (int i = 0; i < self.Count; i++)
                  {
                      retV += Vector3.Distance(self[i], self[(i + 1 < self.Count ? i + 1 : 0)]);
                  }
                 return retV;
              }
             /// <summary>
             /// 计算多边形面积
             /// </summary>
             /// <param name="self">多边形顶点数组</param>
             /// <returns>面积</returns>
             public static float GetPolygonArea(this Vector3[] self)
              {
                 if (self.Length < 3) return 0.0f;
                 float retV = self[0].z * (self[self.Length - 1].x - self[1].x);
                 for (int i = 1; i < self.Length; i++)
                  {
                      retV += self[i].z * (self[i - 1].x - self[(i + 1) % self.Length].x);
                  }
                 return Mathf.Abs(retV / 2.0f);
              }
             /// <summary>
             /// 计算多边形面积
             /// </summary>
             /// <param name="self">多边形顶点列表</param>
             /// <returns>面积</returns>
             public static float GetPolygonArea(this List<Vector3> self)
              {
                 if (self.Count < 3) return 0.0f;
                 float retV = self[0].z * (self[self.Count - 1].x - self[1].x);
                 for (int i = 1; i < self.Count; i++)
                  {
                      retV += self[i].z * (self[i - 1].x - self[(i + 1) % self.Count].x);
                  }
                 return Mathf.Abs(retV / 2.0f);
              }
             /// <summary>
             /// 计算圆的周长
             /// </summary>
             /// <param name="self">半径</param>
             /// <returns>周长</returns>
             public static float GetCirclePerimeter(this float self)
              {
                 return Mathf.PI * 2f * self;
              }
             /// <summary>
             /// 计算圆的面积
             /// </summary>
             /// <param name="self">半径</param>
             /// <returns>面积</returns>
             public static float GetCircleArea(this float self)
              {
                 return Mathf.PI * Mathf.Pow(self, 2);
              }
             /// <summary>
             /// 三角函数计算对边的长度
             /// </summary>
             /// <param name="self">角度</param>
             /// <param name="neighbouringSideLength">邻边的长度</param>
             /// <returns>对边的长度</returns>
             public static float GetFaceSideLength(this float self, float neighbouringSideLength)
              {
                 return neighbouringSideLength * Mathf.Tan(self * Mathf.Deg2Rad);
              }
             /// <summary>
             /// 三角函数计算邻边的长度
             /// </summary>
             /// <param name="self">角度</param>
             /// <param name="faceSideLength">对边的长度</param>
             /// <returns>邻边的长度</returns>
             public static float GetNeighbouringSideLength(this float self, float faceSideLength)
              {
                 return faceSideLength / Mathf.Tan(self * Mathf.Deg2Rad);
              }
             /// <summary>
             /// 勾股定理计算斜边的长度
             /// </summary>
             /// <param name="self">直角边的长度</param>
             /// <param name="anotherRightangleSideLength">另一条直角边的长度</param>
             /// <returns>斜边的长度</returns>
             public static float GetHypotenuseLength(this float self, float anotherRightangleSideLength)
              {
                 return Mathf.Sqrt(Mathf.Pow(self, 2f) + Mathf.Pow(anotherRightangleSideLength, 2f));
              }
             /// <summary>
             /// 正弦
             /// </summary>
             /// <param name="self">角度</param>
             /// <returns>正弦值</returns>
             public static float Sin(this float self)
              {
                 return Mathf.Sin(self * Mathf.Deg2Rad);
              }
             /// <summary>
             /// 余弦
             /// </summary>
             /// <param name="self">角度</param>
             /// <returns></returns>
             public static float Cos(this float self)
              {
                 return Mathf.Cos(self * Mathf.Deg2Rad);
              }
             /// <summary>
             /// 正切
             /// </summary>
             /// <param name="self">角度</param>
             /// <returns>正切值</returns>
             public static float Tan(this float self)
              {
                 return Mathf.Tan(self * Mathf.Deg2Rad);
              }
             /// <summary>
             /// 反正弦
             /// </summary>
             /// <param name="self">正弦值</param>
             /// <returns>角度</returns>
             public static float ArcSin(this float self)
              {
                 return Mathf.Asin(self) * Mathf.Rad2Deg;
              }
             /// <summary>
             /// 反余弦
             /// </summary>
             /// <param name="self">余弦值</param>
             /// <returns>角度</returns>
             public static float ArcCos(this float self)
              {
                 return Mathf.Acos(self) * Mathf.Rad2Deg;
              }
             /// <summary>
             /// 反正切
             /// </summary>
             /// <param name="self">正切值</param>
             /// <returns>角度</returns>
             public static float ArcTan(this float self)
              {
                 return Mathf.Atan(self) * Mathf.Rad2Deg;
              }
             /// <summary>
             /// 度转弧度
             /// </summary>
             /// <param name="self">度</param>
             /// <returns>弧度</returns>
             public static float Deg2Rad(this float self)
              {
                 return self * Mathf.Deg2Rad;
              }
             /// <summary>
             /// 弧度转度
             /// </summary>
             /// <param name="self">弧度</param>
             /// <returns>度</returns>
             public static float Rad2Deg(this float self)
              {
                 return self * Mathf.Rad2Deg;
              }
          }
      }
  
 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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