python - 给定三个坐标点,如何检测它们之间的角度何时超过 180 度?

标签 python math geometry trigonometry angle

这看起来应该很简单,但我遇到了麻烦。基本上,我有三个不断变化的点(我们称它们为 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).

关于python - 给定三个坐标点,如何检测它们之间的角度何时超过 180 度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38856588/

相关文章:

python:打开另一个文件以粘贴输出值

java - 如何找到三个平面的交点?

Python图像旋转角度计算

java - 从三角形的顶点访问区域

python - StringIO 初始值必须是 str,而不是 Bytes

python - 通过记事本在windows中执行python(F6 Exec命令__main__错误)

Python 请求仅返回 1 个元素

python - python中的联合熵

c# - 在类中定义算术运算

python - 如何表示数据的趋势(上升/下降/不变)?