如何用线分割多边形?

时间:2010-09-02 03:50:45

标签: algorithm split polygon

如下图所示,

是否可以用线分割多边形? (分为两个多边形)。如果线没有一直穿过多边形,那么它就会失败。

这可能吗?如果是这样,我该怎么做?

5 个答案:

答案 0 :(得分:16)

我最近不得不这样做。只是走多边形不适用于凹多边形,如图中所示。下面是我的算法草图,受Greiner-Hormann多边形裁剪算法的启发。分割比多边形裁剪更容易和更难。更简单,因为您只剪切线而不是矩形或其他多边形;更难,因为你需要保持双方。

Create an empty list of output polygons
Create an empty list of pending crossbacks (one for each polygon)
Find all intersections between the polygon and the line.
Sort them by position along the line.
Pair them up as alternating entry/exit lines.
Append a polygon to the output list and make it current.
Walk the polygon. For each edge:
    Append the first point to the current polygon.
    If there is an intersection with the split line:
        Add the intersection point to the current polygon.
        Find the intersection point in the intersection pairs.
        Set its partner as the crossback point for the current polygon.
        If there is an existing polygon with a crossback for this edge:
            Set the current polygon to be that polygon.
        Else
            Append a new polygon and new crossback point to the output lists and make it current.
        Add the intersection point to the now current polygon.

答案 1 :(得分:13)

这是可能的,但如果多边形不是凸面,那么将它分割成一条线可能会导致两个以上的多边形。

遍历多边形边,并为每条边确定它是否与线相交。找到第一个这样做的边缘。继续遍历,直到找到另一个这样的边,但是将沿途遇到的每个边添加到一个新的多边形(包括第一条边的一部分,该部分由“此边”上的线分开,同样适用于最后一条边)。最后,为新的Polygon添加一个结束边。现在继续处理边缘 - 在线的另一侧,以相同的方式创建另一个多边形。您现在有两个多边形分割线。如果您小心,将非凸多边形分割成多个多边形,相同的技术将起作用。

请注意角落情况,例如穿过多边形顶点的直线,以及根本不与多边形相交的直线。

编辑:正如xan指出的那样,这将无法正确处理所有非凸案例。这可以通过对算法的小修改来修复。在如上所述添加结束边之前,必须首先检查原始多边形的任何其他边是否与该结束边相交;如果是这样,您只关闭该边缘并继续处理该点的其他边缘。

答案 2 :(得分:2)

你需要的只是一个多边形裁剪alogrithem。您可以在此处查看概述: Polygon clipping我认为有很多可以学习的实施方法。

答案 3 :(得分:1)

完全可能。我假设你正在使用Java2d。你在其中找到一个名为intersects的方法。使用它你可以做到这一点。

您可能必须修改多边形的this实现并再写一个交叉方法,该方法传递Line2D对象并对其进行自定义以使其传递数组多边形(可能因为相同的线切割会产生无限多边形 - 假设一个之字形多边形)或null。

答案 4 :(得分:1)

1994年,George Vanecek用3D制作了一个解决方案,并在平面Gems V"平面多边形空间划分"中发布了解决方案。源代码仍可在Graphic Gems Repository中找到。

最近,David Geier发布了Vanecek算法的2D实现,并对算法进行了解释。见David's Blog: Splitting an arbitrary polygon by a line