网站首页 > 开源技术 正文
总想默默记住这些好用数学函数
看到许多大神都分享数学库函数的文章,小编还是忍不住重复把它默默收集在经验目录中。
Mathf 数学函数库浅析
1、Mathf.Abs 绝对值
计算并返回指定参数 f 绝对值。
2、Mathf.Acos 反余弦
static function Acos (f : float) : float
以弧度为单位计算并返回参数 f 中指定的数字的反余弦值。
3、Mathf.Approximately 近似
static function Approximately (a : float, b: float) : bool
比较两个浮点数值,看它们是否非常接近, 由于浮点数值不精确,不建议使用等于来比较它们。例如,1.0==10.0/10.0也许不会返回true。
public class example : MonoBehaviour { public void Awake() { if(Mathf.Approximately(5.0F, 50.0F / 50.0F)) print("近似"); } }
4、Mathf.Asin 反正弦
static function Asin (f : float) : float
以弧度为单位计算并返回参数 f 中指定的数字的反正弦值。
5、Mathf.Atan2 反正切
static function Atan2 (y : float, x :float) : float
以弧度为单位计算并返回 y/x 的反正切值。返回值表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。
返回值是在 x 轴和一个二维向量开始于 0 个结束在 (x,y) 处之间的角。
public class example : MonoBehaviour { public Transform target; void Update() { Vector3 relative = transform.InverseTransformPoint(target.position); float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg; transform.Rotate(0,angle, 0); } }
6、Mathf.Atan 反正切
static function Atan (f : float) :float
计算并返回参数 f 中指定的数字的反正切值。返回值介于负二分之 pi 与正二分之 pi 之间。
7、Mathf.CeilToInt 最小整数
static function CeilToInt (f : float) : int
返回最小的整数大于或等于f。
8、Mathf.Ceil 上限值
static function Ceil (f : float) : float
返回 f 指定数字或表达式的上限值。数字的上限值是大于等于该数字的最接近的整数。
9、Mathf.Clamp01 限制 0~1
static function Clamp01 (value : float) :float
限制 value 在 0,1 之间并返回 value 。如果 value 小于 0 ,返回 0。如果 value 大于 1 ,返回 1 ,否则返回 value 。
10、Mathf.Clamp 限制
static function Clamp (value : float, min :float, max : float) : float
限制 value 的值在 min 和 max 之间, 如果 value 小于 min ,返回 min 。 如果 value 大于 max ,返回 max ,否则返回 value 。
static function Clamp (value : int, min :int, max : int) : int
限制 value 的值在 min 和 max 之间,并返回 value 。
11、Mathf.ClosestPowerOfTwo 最近的二次方
static function ClosestPowerOfTwo (value :int) : int
返回距离value最近的2的次方数。
12、Mathf.Cos 余弦
static function Cos (f : float) : float
返回由参数 f 指定的角的余弦值(介于 -1.0 与 1.0 之间的值)。
13、Mathf.Deg2Rad 度转弧度
static var Deg2Rad : float
度到弧度的转化常量。(只读)
这等于(PI * 2) / 360。
14、Mathf.Mathf.Rad2Deg 弧度转度
static var Rad2Deg : float
弧度到度的转化常量。(只读)
这等于 360 / (PI * 2)。
15、Mathf.DeltaAngle 增量角
static function DeltaAngle (current :float, target : float) : float
计算给定的两个角之间最短的差异。
// Prints 90 Debug.Log(Mathf.DeltaAngle(1080,90));
16、Mathf.Epsilon 小正数
static var Epsilon : float
一个很小的浮点数值。(只读)
最小的浮点值,不同于0。
以下规则:
- anyValue + Epsilon = anyValue
- anyValue - Epsilon = anyValue
- 0 + Epsilon = Epsilon
- 0 - Epsilon = -Epsilon
一个在任意数和Epsilon的之间值将导致在任意数发生截断误差。
public class example : MonoBehaviour { bool isEqual(float a, float b) { if(a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon) return true; else return false; } }
17、Mathf.Exp 指数
static function Exp (power : float) : float
返回 e 的 power 次方的值。
18、Mathf.FloorToInt 最大整数
static function FloorToInt (f : float) :int
返回最大的整数,小于或等于f。
19、Mathf.Floor 下限值
static function Floor (f : float) : float
返回参数 f 中指定的数字或表达式的下限值。下限值是小于等于指定数字或表达式的最接近的整数。
20、Mathf.Infinity 正无穷
static var Infinity : float
表示正无穷,也就是无穷大,∞ (只读)
21、Mathf.InverseLerp 反插值
计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。
//现在参数是3/5 float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);
22、Mathf.IsPowerOfTwo 是否 2 的幂
static function IsPowerOfTwo (value : int): bool
如果该值是 2 的幂,返回 true 。
// prints false Debug.Log(Mathf.IsPowerOfTwo(7)); // prints true Debug.Log(Mathf.IsPowerOfTwo(32));
23、Mathf.LerpAngle 插值角度
static function LerpAngle (a : float, b :float, t : float) : float
和 Lerp 的原理一样,当他们环绕 360 度确保插值正确。
a和b是代表度数。
public class example : MonoBehaviour { public float minAngle = 0.0F; public float maxAngle = 90.0F; void Update() { float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time); transform.eulerAngles= new Vector3(0, angle, 0); } } }
24、Mathf.Lerp 插值
static function Lerp (from : float, to :float, t : float) : float
基于浮点数t返回 a 到 b 之间的插值,t限制在 0~1 之间。
当 t = 0 返回 from ,当 t = 1 返回 to 。当 t = 0.5 返回 from 和 to 的平均值。
25、Mathf.Log10 基数10的对数
static function Log10 (f : float) : float
返回f的对数,基数为10。
26、Mathf.Log 对数
static function Log (f : float, p : float): float
返回参数 f 的对数。
// logarithm of 6 in base 2 //以2为底6的对数 // prints 2.584963 print(Mathf.Log(6, 2));
27、Mathf.Max 最大值
static function Max (a : float, b : float): float
static function Max (params values :float[]) : float
返回两个或更多值中最大的值。
28、Mathf.Min 最小值
static function Min (a : float, b : float): float
static function Min (params values :float[]) : float
返回两个或更多值中最小的值。
29、Mathf.MoveTowardsAngle 移动角
static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float
像 MoveTowards ,但是当它们环绕 360 度确保插值正确。
变量 current 和 target 是作为度数。为优化原因,maxDelta 负值的不被支持,可能引起振荡。从target 角推开 current ,添加 180 度角代替。
30、Mathf.MoveTowards 移向
static function MoveTowards (current :float, target : float, maxDelta : float) : float
改变一个当前值向目标值靠近。
这实际上和 Mathf.Lerp 相同,而是该函数将确保我们的速度不会超过 maxDelta 。maxDelta 为负值将目标从推离。
31、Mathf.NegativeInfinity 负无穷
static var NegativeInfinity : float
表示负无穷,也就是无穷小,-∞(只读)
32、Mathf.NextPowerOfTwo 下个2的幂
//Prints 8 to the console Debug.Log(Mathf.NextPowerOfTwo(7));
33、Mathf.PingPong 乒乓
static function PingPong (t : float, length: float) : float
0 到 length 之间往返。t 值永远不会大于 length 的值,也永远不会小于 0 。
The returned value will move back and forthbetween 0 and length.
返回值将在 0 和 length 之间来回移动。
34、Mathf.PI 圆周率
static var PI : float
PI(读pai)的值,也就是圆周率(π)的值3.14159265358979323846...(只读)
35、Mathf.Pow 次方
static function Pow (f : float, p : float): float
计算并返回 f 的 p 次方。
36、Mathf.Repeat 重复
static function Repeat (t : float, length :float) : float
循环数值 t , 0 到 length 之间。 t 值永远不会大于 length 的值,也永远不会小于 0 。
这是类似于模运算符,但可以使用浮点数。
public class example : MonoBehaviour { void Update() { transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z); } }
37、Mathf.RoundToInt 四舍五入到整数
static function RoundToInt (f : float) :int
返回 f 指定的值四舍五入到最近的整数。
如果数字末尾 0.5 ,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。
38、Mathf.Round 四舍五入
static function Round (f : float) : float
返回浮点数 f 进行四舍五入最接近的整数。
如果数字末尾是 0.5 ,因此它是在两个整数中间,不管是偶数或是奇数,将返回偶数。
39、Mathf.Sign 符号
static function Sign (f : float) : float
返回 f 的符号。
当 f 为正或为0返回1,为负返回-1。
40、Mathf.Sin 正弦
static function Sin (f : float) : float
计算并返回以弧度为单位指定的角 f 的正弦值。
41、Mathf.SmoothDampAngle 平滑阻尼角度
static function SmoothDampAngle (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float
参数
current :当前的位置。
target :我们试图达到的位置。
currentVelocity :当前速度,这个值在你访问这个函数的时候会被随时修改。
smoothTime( the target faster) :要到达目标位置的近似时间,实际到达目标时要快一些。
maxSpeed :可选参数,允许你限制的最大速度。
deltaTime :上次调用该函数到现在的时间。缺省为Time.deltaTime。
随着时间的推移逐渐改变一个给定的角度到期望的角度。 这个值通过一些弹簧减震器类似的功能被平滑。这个函数可以用来平滑任何一种值,位置,颜色,标量。最常见的是平滑一个跟随摄像机。
//一个简单的平滑跟随摄像机 //跟随目标的朝向 public class example : MonoBehaviour { public Transform target; public float smooth = 0.3F; public float distance = 5.0F; private float yVelocity = 0.0F; void Update() { //从目前的y角度变换到目标y角度 float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y,ref yVelocity, smooth); //target的位置 Vector3 position = target.position; //然后,新角度之后的距离偏移 position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance); //应用位置 transform.position= position; //看向目标 transform.LookAt(target); } }
42、Mathf.SmoothDamp 平滑阻尼
static function SmoothDamp (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float
参数
current :当前的位置。
target :我们试图达到的位置。
currentVelocity :当前速度,这个值在你访问这个函数的时候会被随时修改。
smoothTime :要到达目标位置的近似时间,实际到达目标时要快一些。
maxSpeed :可选参数,允许你限制的最大速度。
deltaTime :上次调用该函数到现在的时间。缺省为Time.deltaTime。
描述
随着时间的推移逐渐改变一个值到期望值。
这个值就像被一个不会崩溃的弹簧减振器一样被平滑。这个函数可以用来平滑任何类型的值,位置,颜色,标量。
public class example : MonoBehaviour { public Transform target; public float smoothTime = 0.3F; private float yVelocity = 0.0F; void Update() { float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity, smoothTime); transform.position= new Vector3(transform.position.x, newPosition, transform.position.z); } }
43、Mathf.SmoothStep 平滑插值
static function SmoothStep (from : float,to : float, t : float) : float
和lerp类似,在最小和最大值之间的插值,并在限制处渐入渐出。
public class example : MonoBehaviour { public float minimum = 10.0F; public float maximum = 20.0F; void Update() { transform.position= new Vector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0); } }
44、Mathf.Sqrt 平方根
static function Sqrt (f : float) : float
计算并返回 f 的平方根。
45、Mathf.Tan 正切
static function Tan (f : float) : float 计算并返回以弧度为单位 f 指定角度的正切值。
Unity3D 官方数学库学习教程:
https://docs.unity3d.com/ScriptReference/Mathf.html
C# 中常用的 Math 函数图表
C# Math 图表
猜你喜欢
- 2024-12-13 外媒说 | 看看外媒都是如何评价《芭比》这部电影的
- 2024-12-13 用js实现html页面水印
- 2024-12-13 GC-PowerStation使用教程
- 2024-12-13 2023 年 CSS 新特性大盘点
- 2024-12-13 Canvas 从进阶到退学
- 2024-12-13 Qt4/5升级到Qt6吐血经验总结V202308
- 2024-12-13 肝一下Go.js的高级使用—动态显示效果......
- 2024-12-13 如何用FFMpeg生成视频
- 2024-12-13 如何在fabric.js中使用蒙版合成图片
- 2024-12-13 canvas画箭头线
你 发表评论:
欢迎- 05-16东契奇:DFS训练时喷了我很多垃圾话 我不懂他为什么比赛不这么干
- 05-16这两球很伤!詹姆斯空篮拉杆不中 DFS接里夫斯传球空接也没放进
- 05-16湖人自媒体调查:89%球迷希望DFS回归79%希望詹姆斯回归
- 05-16Shams:湖人得到全能球员DFS 节省了1500万奢侈税&薪金空间更灵活
- 05-16G5湖人胜率更高!詹姆斯不满判罚,DFS谈5人打满下半场:这很艰难
- 05-16DFS:当东契奇进入状态 所有防守者在他面前都像个圆锥桶
- 05-16上一场9中6!DFS:不能让纳兹-里德这样的球员那么轻松地投三分
- 05-16WIDER FACE评测结果出炉:滴滴人脸检测DFS算法获世界第一
- 最近发表
-
- 东契奇:DFS训练时喷了我很多垃圾话 我不懂他为什么比赛不这么干
- 这两球很伤!詹姆斯空篮拉杆不中 DFS接里夫斯传球空接也没放进
- 湖人自媒体调查:89%球迷希望DFS回归79%希望詹姆斯回归
- Shams:湖人得到全能球员DFS 节省了1500万奢侈税&薪金空间更灵活
- G5湖人胜率更高!詹姆斯不满判罚,DFS谈5人打满下半场:这很艰难
- DFS:当东契奇进入状态 所有防守者在他面前都像个圆锥桶
- 上一场9中6!DFS:不能让纳兹-里德这样的球员那么轻松地投三分
- WIDER FACE评测结果出炉:滴滴人脸检测DFS算法获世界第一
- 湖人自媒体调查:89%球迷希望DFS回归 79%希望詹姆斯回归
- 一觉醒来湖人苦盼的纯3D终于到位 DFS能带给紫金军多少帮助
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)