确定线段是否与多边形相交
如果我在 2D 平面上有一个向量(由 2 个点组成的线),我如何确定它是否穿过多边形?
我知道我可以采用构成多边形的每条线并查看是否有相交,但有更好的方法吗?
我读过这篇文章 如何确定 2D 点是否在多边形内? 这给了我一些关于查看该点是否在多边形内的想法,但我需要查看它是否已经经过/相交。
If I have a vector (a line consisting of 2 points) on a 2D plane how can I determine if it has passed through a polygon?
I know I can take each line which makes up the polygon and see if any intersect but is there a better way?
I've read this one post How can I determine whether a 2D Point is within a Polygon? which gives me some ideas for seeing whether the point is within a polygon but I need to see if it has passed over/intersected it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想要一个用于几何运算的 Python 库,请查看
shapely
。它使得这就像someline.intersects(somepolygon)
一样简单。这是一个交叉点、缓冲区和裁剪的快速示例(有一个很好的图...我正在使用
descartes
轻松将形状多边形转换为 matplotlib 补丁。)。这会产生:
If you want a python library for geometric operations, have a look at
shapely
. It makes this as simple assomeline.intersects(somepolygon)
.Here's a quick example of intersections, buffer, and clipping (with a nice plot... I'm using
descartes
to easily convert shapely polygons into matplotlib patches.).This yields:
当且仅当线穿过多边形的一条边时,线才穿过多边形(暂时忽略穿过顶点时的情况)。因此,在您的情况下,您只需要根据您的线测试多边形的所有边,看看是否有交叉点。
测试边
(a, b)
是否与直线相交很容易。只需按照以下形式为直线建立直线方程,然后计算点
a
和b
的值Ax + By + C
。如果a
和b
的这些值的符号不同,则边(a, b)
与线相交。剩下的就是找出一种方法来处理线经过顶点(边的端点)时的情况,但这很容易做到。
Line crosses the polygon if and only if it crosses one of its edges (ignoring for a second the cases when it passes through a vertex). So, in your case you just need to test all edges of your polygon against your line and see if there's an intersection.
It is easy to test whether an edge
(a, b)
intersects a line. Just build a line equation for your line in the following formand then calculate the value
Ax + By + C
for pointsa
andb
. If the signs of theses values fora
andb
are different, then edge(a, b)
intersects the line.All that's left is to work out a way to handle the cases when the line passes through a vertex (endpoint of an edge), but it is easy to do.
如果您不太关心效率,则可以简单地测试给定两个参考点和多边形上所有相邻点对的线相交。一旦检测到交叉点,您就知道您的线与多边形相交。
一如既往,维基百科是一个很好的起点: http://en.wikipedia.org/wiki/ Line-line_intersection
因此,让我们运行一个示例
,并且不要忘记使用 (qn, q0) 循环来关闭多边形!
祝你好运!
If you don't care too much about efficiency, you can simply test for line intersection given your two reference points and all adjacent point pairs on the polygon. Once you detect an intersection, you know that your line intersects with the polygon.
A good starting point is, as always, wikipedia: http://en.wikipedia.org/wiki/Line-line_intersection
So, lets run through an example
And don't forget to cycle around with ( qn, q0 ) to close the poly!
Good luck!
多边形算法中不是有一个快速的点吗?使用一,您可以确定其中一个点是否恰好在内部,这也可能意味着相交。如果它们都位于外面,则仍然需要使用其他方法之一。
Isn't there a quick is point in polygon algorithm? Using one, you could determine if exactly one of the points is inside, which might also means intersection. If they both lie outside, one of the other methods is still required.
此答案基于Joe Kington 对此问题的回答。
我修改了代码,使其可以与 Python 3 一起使用。还有一个问题是 shaply/numpy/matplotlib 的当前实现与代码不兼容。笛卡尔还有另一个问题。该版本不依赖于笛卡尔。我试图重现相同的外观。
This answer is based on an answer by Joe Kington to this question.
I have modified the code such that it is working with Python 3. There also was the problem that the current implementation of shaply/numpy/matplotlib is not compatible with the code. There also was another problem with descartes. This version does not depend on descartes. I have tried to reproduce the same look.