我想要一个简单的解决方案来计算一条线的角度(就像时钟的指针)。
我有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
呃,请注意,上面显然不是编译 Javascript 代码。您必须查看文档反正切 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan功能。
Edit: Using 数学.atan2(y,x) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2将为您处理所有特殊情况和额外逻辑:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)