给定三个坐标点,如何检测它们之间的角度何时超过 180 度?
Given three coordinate points, how do you detect when the angle between them crosses 180 degrees?
这看起来应该很简单,但我遇到了麻烦。基本上,我有三个不断变化的点(我们称它们为 p1、p2 和 p3)。另外,让我们将 p2 定义为顶点。
本质上,我需要做的是计算三个点之间的角度。一个很好的例子是,如果三个角形成一个 179 度角,那么这些点就会变成一个 181 度角。所以我真正需要的是一种确定角度是否大于 180 度的好方法。我尝试使用余弦定律,但它没有给我一个好的答案,因为当点形成 181 度角时,它只是将它解释为不同方向的 179 度角。此外,如果有帮助,我将在 Python 中执行此操作。谢谢!
您要判断的是 (p3-p2) 与 (p2-p1) 相比是左转还是右转。这实际上是Graham Scan的核心部分,用于计算凸包(https://en.wikipedia.org/wiki/Graham_scan)。引用维基百科稍加编辑:
...determining whether three points constitute a "left turn" or a
"right turn" does not require computing the actual angle between the
two line segments, and can actually be achieved with simple arithmetic
only. For three points P1=(x1, y1), P2=(x2, y2), and P3=(x3, y3),
simply compute the z-coordinate of the cross product of the two
vectors (p2-p1) and (p3-p1), which is given by the expression
(x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
. If the result is 0, the
points are collinear; if it is positive, the three points constitute a
"left turn" or counter-clockwise orientation, otherwise a "right turn"
or clockwise orientation (for counter-clockwise numbered points).
要获得整个范围内的符号角,请使用 atan2 函数与向量 p2p1
和 p2p3
的点积和叉积
Angle(in radians) = atan2(cross(p2p1,p2p3), dot(p2p1,p2p3))
这看起来应该很简单,但我遇到了麻烦。基本上,我有三个不断变化的点(我们称它们为 p1、p2 和 p3)。另外,让我们将 p2 定义为顶点。
本质上,我需要做的是计算三个点之间的角度。一个很好的例子是,如果三个角形成一个 179 度角,那么这些点就会变成一个 181 度角。所以我真正需要的是一种确定角度是否大于 180 度的好方法。我尝试使用余弦定律,但它没有给我一个好的答案,因为当点形成 181 度角时,它只是将它解释为不同方向的 179 度角。此外,如果有帮助,我将在 Python 中执行此操作。谢谢!
您要判断的是 (p3-p2) 与 (p2-p1) 相比是左转还是右转。这实际上是Graham Scan的核心部分,用于计算凸包(https://en.wikipedia.org/wiki/Graham_scan)。引用维基百科稍加编辑:
...determining whether three points constitute a "left turn" or a "right turn" does not require computing the actual angle between the two line segments, and can actually be achieved with simple arithmetic only. For three points P1=(x1, y1), P2=(x2, y2), and P3=(x3, y3), simply compute the z-coordinate of the cross product of the two vectors (p2-p1) and (p3-p1), which is given by the expression
(x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
. If the result is 0, the points are collinear; if it is positive, the three points constitute a "left turn" or counter-clockwise orientation, otherwise a "right turn" or clockwise orientation (for counter-clockwise numbered points).
要获得整个范围内的符号角,请使用 atan2 函数与向量 p2p1
和 p2p3
Angle(in radians) = atan2(cross(p2p1,p2p3), dot(p2p1,p2p3))