在二维空间中从点A到点B? [英] Heading from Point A to Point B in 2D space?
问题描述
我正在研究一个项目,该项目要求我计算从0到360度范围内的变量点A到变量点B的标题,让点A上的对象面向点B。
现在,我不确定如何实现这一目标,但我没有找到任何好的解决方案。
如何计算在任何情况下从二维空间的点A到点B的标题?解决方案
在诸如C或C ++的语言中,你可能会使用 atan2
函数,该函数计算四个象限上y / x的反正切值,考虑x和y的符号。
如果A在(x1,y1)和B在(x2,y2),那么以弧度表示的标题由下式给出:
theta_radians = atan2(y2 - y1,x2 - x1);
theta_radians
的范围是-π到+π。您可以将其转换为0到360之间的度数,如下所示:
theta_degrees =(theta_radians + M_PI)* 360.0 /( 2.0 * M_PI);
I'm working on a project which requires me to calculate the heading from a variable point A to a variable point B in 0 - 360 degrees to let the object at point A face point B.
Now, I'm unsure on how to achieve this, I googled but didn't find any good solution.
How would I calculate the heading from point A to point B in 2D space in any situation?
In a language such as C or C++ you might use the atan2
function, which calculates the arctangent of y/x over four quadrants, taking the signs of x and y into account.
If A is at (x1, y1) and B is at (x2, y2), then the heading in radians is given by:
theta_radians = atan2(y2 - y1, x2 - x1);
The range of theta_radians
is -π to +π. You can convert this to degrees in the range 0 to 360 as follows:
theta_degrees = (theta_radians + M_PI) * 360.0 / (2.0 * M_PI);
这篇关于在二维空间中从点A到点B?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!