常用的三角函数方法

角度与弧度互转

/** 
* 根据角度求弧度。 
* Math.PI 相当于一个半圆,除以180°可以求出1°是多少弧度。 
*/
function getRadian (degree) {
 return degree * Math.PI / 180
}
/**
 * 根据弧度求角度。
 * 180°除以Math.PI可以求出1弧度等于多少角度。
 */
function getDegree (radian) {
 return radian * 180 / Math.PI
}

正弦

Math.sin - 对边与斜边的比率。

// 30°角的弧度
const radian = getRadian(30) // 0.5235987755982988
// 参数为弧度
Math.sin(radian) // 0.49999999999999994

余弦

Math.cos - 邻边与斜边的比率。

// 求30°角余弦值
Math.cos(getRadian(30)) // 0.8660254037844387

正弦余弦的应用

线性垂直运动:

脉冲运动:

圆周运动:

运动轨迹:

正切

Math.tan - 对边与邻边的比率。

// 求30°角正切值
Math.tan(getRadian(30)) // 0.5773502691896257

反正弦/反余弦/反正切

Math.asin/Math.acos/Math.atan - 正弦余弦正切的逆运算,输入一个比率可获得对应的角的弧度。

// 30°角正弦值为0.5,通过反正弦计算求出弧度,再转成角度刚好为30°
const radian = Math.asin(0.5) // 得到弧度:0.5235987755982989
getDegree(radian) // 得到角度:30.000000000000004

还有个 Math.atan2(y,x) ,通过对边长度 y 和 邻边长度 x 计算对应弧度。

反正切的应用