我有以下函数,可以找到 3 点之间的 Angular 。
(其中一个点被定义为一个数组,其中包含 x 位置作为其第一个元素,y 位置作为其第二个元素,要测量的 Angular 是通过所有三个点绘制一条线时创建的 Angular 。)
findAngle: function(a, b, c) {
var ab = Math.sqrt(Math.pow(b[0] - a[0], 2) + Math.pow(b[1] - a[1], 2));
var bc = Math.sqrt(Math.pow(b[0] - c[0], 2) + Math.pow(b[1] - c[1], 2));
var ac = Math.sqrt(Math.pow(c[0] - a[0], 2) + Math.pow(c[1] - a[1], 2));
var o1 = (bc * bc + ab * ab - ac * ac) / (2 * bc * ab);
var o2 = Math.acos(o1);
var o3 = o2 * (180 / Math.PI);
return o3;
}
但是,这只会返回 0 到 180 度之间的 Angular 。当 Angular 实际上是钝 Angular 时,如何让它返回 0 - 180 度之间的值,以及 180 到 360 度之间的值?
最佳答案
使用具有两个参数的 atan2 函数,并返回 -Pi..Pi
范围内的结果(如果需要,将 2*Pi
添加到负 Angular )
angle = Math.atan2(crossproduct(c-b,b-a), dotproduct(c-b,b-a))
where
crossproduct(c-b,b-a) = (c.x-b.x)*(b.y-a.y) - (c.y-b.y)*(b.x-a.x)
dotproduct(c-b,b-a) = (c.x-b.x)*(b.x-a.x) + (c.y-b.y)*(b.y-a.y)
关于javascript - 求三点之间的 Angular ,([x,y], [x,y], [x,y]) 允许钝 Angular 结果。 (数学和 JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43297780/