数学计算两点之间的夹角找回? [英] Math Calculation to retrieve angle between two points?
问题描述
可能重复:结果
的如何计算相对于水平轴的两个点之间的角度θ
我一直在寻找这样的年龄,它只是真的很烦我,所以我决定只问...
I've been looking for this for ages and it's just really annoying me so I've decided to just ask...
所提供的,我有两个点(即X1,Y1,和X2,Y2),我想以计算这两个点之间的角度,假设,当Y1 == y 2和X1> X2角度为180度...
Provided I have two points (namely x1, y1, and x2, y2), I would like to calculate the angle between these two points, presuming that when y1 == y2 and x1 > x2 the angle is 180 degrees...
我有下面的代码,我一直在与(使用从高中的知识),我只是似乎无法。产生期望的结果。
I have the below code that I have been working with (using knowledge from high school) and I just can't seem to produce the desired result.
float xDiff = x1 - x2;
float yDiff = y1 - y2;
return (float)Math.Atan2(yDiff, xDiff) * (float)(180 / Math.PI);
谢谢,我得到如此沮丧...
Thanks in advance, I'm getting so frustrated...
推荐答案
从我收集了,你想以下举行:
From what I've gathered, you want the following to hold:
- 水平分割线:
P1 P2 --------
=> 0° - 水平分割线:
P2 -------- P1
=> 180°
- Horizontal line:
P1 -------- P2
=> 0° - Horizontal line:
P2 -------- P1
=> 180°
您说,您想要的角度,顺时针方向增加。
You said, you want the angle to increase in clockwise direction.
旋转此行 P1 P2 --------
,使得 P1
超过 P2
中,角度必须从而在90°
Rotating this line P1 -------- P2
such that P1
is above P2
, the angle must thus be 90°.
如果,然而,我们在相反的方向转动时, P1
将低于 P2
和角度是90°或270°。
If, however, we rotated in the opposite direction, P1
would be below P2
and the angle is -90° or 270°.
依据:考虑到 P1
为原点和测量 P2
相对的角度原点,那么 P1 P2 -------- $ C $ 。C>将正确产生
0
Basis: Considering P1
to be the origin and measuring the angle of P2
relative to the origin, then P1 -------- P2
will correctly yield 0
.
float xDiff = x2 - x1;
float yDiff = y2 - y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
然而, ATAN2
让我们的角度增加CCW方向。
逆时针方向绕原点旋转,是
经过以下值:
However, atan2
let's the angle increase in CCW direction.
Rotating in CCW direction around the origin, y
goes through the following values:
- Y = 0
- Y> 0
- Y = 0
- Y'LT; 0
- Y = 0
- y = 0
- y > 0
- y = 0
- y < 0
- y = 0
这意味着,我们可以简单地反转的迹象是
翻转方向。但由于C#的坐标从从上到下依次增加,符号已经计算时, yDiff
逆转。
This means, that we can simply invert the sign of y
to flip the direction. But because C#'s coordinates increase from top to bottom, the sign is already reversed when computing yDiff
.
这篇关于数学计算两点之间的夹角找回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!