两个凸多边形的交点

问题描述 投票:12回答:3

我有两个凸多边形。多边形实现为其顶点的循环列表。如何找到这两个多边形的交点?

algorithm graphics polygon
3个回答
9
投票
For each edge V1-V2 in the first polygon,
    Let H := Half-plane tangenting V1-V2, with the remaining
        vertices on the "inside".
    Let C := New empty polygon.
    For each edge V3-V4 in the second polygon,
        Let X := The intersection between V3-V4 and H.
        If V3 inside H, and V4 is outside H then,
            Add V3 to C.
            Add X to C.
        Else if both V3 and V4 lies outside H then,
            Skip.
        Else if V3 outside H, and V4 is inside H then,
            Add X to C.
        Else
            Add V3 to C.
    Replace the second polygon with C.

这应该足以简单使用; 10-20个顶点,不重新计算每一帧。 - O(n2)


这是一些链接:


6
投票

您可以从两个多边形都是凸面的事实中受益。有了这些知识,您可以使用以下扫描线算法实现O(n)时间:

找到两个多边形中的最顶点。为简单起见,假设您没有水平边。创建有助于多边形左右边界的边列表。

在扫平飞机时,您可以存储4条边。 left_edge_C1right_edge_C1left_edge_C2right_edge_C2。你不需要任何复杂的方法来磨边,因为它们只有四个。您可以通过迭代所有可能的选项来找到下一个事件点。

在每个事件点处,一些边缘出现在它们的交叉点的边界上。基本上,在每个事件点你可以有三种情况之一(见图)。


2
投票

除了@Yola的漂亮的平面扫描描述之外,还有Computational Geometry in C第7章中描述的线性时间算法.C&Java代码可用(在同一链接上)。有几种棘手的退化情况,例如,当两个多边形在一个点相交时,或者交叉点是一个分段。

© www.soinside.com 2019 - 2024. All rights reserved.