javascript - 如何从点计算 Angular ?

标签 javascript function coordinates angle

我想得到一个简单的解决方案来计算一条线的 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/

相关文章:

firebase - 'String'类型不是 'double'类型的子类型

javascript - ResponsiveVoice.js 无法与 Phonegap 编译器一起正常工作

javascript - 探戈与 django "Like Button"不工作

javascript - 如何在 JavaScript 中用 %20 替换空格?

php - 在 Javascript 代码中使用 PHP 变量

r - R 类的顺序会导致什么?

arrays - 从数组中生成坐标

javascript - Handlebars 模板未正确迭代

c - "Array"功能正在阻止程序正常运行

Python根据距离均匀分散固定中心周围的坐标