C++ 计算 2 点的角度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2339487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate Angle of 2 points
提问by jmasterx
Given P1 and P2, how can I get the angle from P1 to P2? Thanks
给定 P1 和 P2,我怎样才能得到从 P1 到 P2 的角度?谢谢
回答by Hyman
It's just float angle = atan2(p1.y - p2.y, p1.x - p2.x)
.
这只是float angle = atan2(p1.y - p2.y, p1.x - p2.x)
。
Of course the return type is in radians, if you need it in degrees just do angle * 180 / PI
当然,返回类型是弧度,如果你需要它的度数就可以了 angle * 180 / PI
回答by Ankur
Ok remembering high school trig. this is what I get.
好的记得高中的三角恋。这就是我得到的。
Two points are A(x1,y1) and B(x2,y2)
两点是 A(x1,y1) 和 B(x2,y2)
I assume you want the angle between the two points and the origin O(0,0).
我假设您想要两点与原点 O(0,0) 之间的角度。
Well each point makes a triangle bounded by its height, its base and its hypotenuse, so you get two angles alpha1 and alpha2. The idea is to find each of these and compute your required angle beta, by doing beta = alpha1 - alpha2 where alpha1 is such that alpha1 > alpha2.
好吧,每个点都构成一个以高度、底边和斜边为边界的三角形,所以你会得到两个角 alpha1 和 alpha2。这个想法是通过执行 beta = alpha1 - alpha2 其中 alpha1 使得 alpha1 > alpha2 来找到这些中的每一个并计算您所需的角度 beta。
Compute alpha1 = inv_tan(y1/x1) and alpha2 = inv_tan(y2/x2)
计算 alpha1 = inv_tan(y1/x1) 和 alpha2 = inv_tan(y2/x2)
then do beta = alpha1 - alpha2
然后做 beta = alpha1 - alpha2