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