Unity Mathf【Deg & Rad】- 关于数学运算中的度与弧度

        Mathf中的三角函数是我们常用的数学运算函数,在使用过程中需要注意参数的单位,以Sin正弦函数为例,我们都知道30度角的正弦函数结果为0.5,那么当我们调用Mathf.Sin函数时,假如我们传入30,则可以发现其结果并不是0.5:

Debug.Log($"Mathf.Sin(30) => {Mathf.Sin(30f)}");

原因是其参数并不是以Degree度为单位,而是以Radians弧度为单位:

// 摘要:
//     Returns the sine of angle f.
// 参数:
//   f:
//     The input angle, in radians.
// 返回结果:
//     The return value between -1 and +1.
public static float Sin(float f);

        那么什么是弧度,百科词条中这样定义:弧长等于半径的弧,其所对的圆心角为1弧度。(即两条射线从圆心向圆周射出,形成一个夹角和夹角正对的一段弧。当这段弧长正好等于圆的半径时,两条射线的夹角的弧度为1)。

        根据定义,一周的弧度数为2πr/r,即2π,那么1度等于2π/360,约等于0.01745弧度。

        因此我们在调用Mathf.Sin时,假设角度为30度,那么需要乘以近似值0.01745再作为参数传入,Mathf类中定义了这个常量,即Deg2Rad(度转弧度):

// 摘要:
//     Degrees-to-radians conversion constant (Read Only).
public const float Deg2Rad = 0.0174532924F;
Debug.Log($"Mathf.Sin(30f * Mathf.Deg2Rad) => {Mathf.Sin(30f * Mathf.Deg2Rad)}");

Mathf中同样定义了弧度转度的常量,Rad2Deg:

// 摘要:
//     Radians-to-degrees conversion constant (Read Only).
public const float Rad2Deg = 57.29578F;

那么我们在使用反正弦函数时,需要用到该常量:

// 摘要:
//     Returns the arc-sine of f - the angle in radians whose sine is f.
// 参数:
//   f:
public static float Asin(float f);
Debug.Log($"Mathf.Asin(0.5f) * Mathf.Rad2Deg => {Mathf.Asin(0.5f) * Mathf.Rad2Deg}");

   欢迎关注公众号 “当代野生程序猿”

猜你喜欢

转载自blog.csdn.net/qq_42139931/article/details/123269901