我想得到一个简单的解决方案来计算一条线的 Angular (就像时钟的指针)。
我有2点:
cX, cY - the center of the line.
eX, eY - the end of the line.
The result is angle (0 <= a < 360).
哪个函数能够提供这个值?
最佳答案
你想要反正切:
dy = ey - cy
dx = ex - cx
theta = arctan(dy/dx)
theta *= 180/pi // rads to degs
Erm,注意上面明显不是在编译Javascript代码。您必须查看 arctangent 的文档功能。
编辑:使用Math.atan2(y,x)将为您处理所有特殊情况和额外逻辑:
function angle(cx, cy, ex, ey) {
var dy = ey - cy;
var dx = ex - cx;
var theta = Math.atan2(dy, dx); // range (-PI, PI]
theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
//if (theta < 0) theta = 360 + theta; // range [0, 360)
return theta;
}
关于javascript - 如何从点计算 Angular ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9614109/