Algorithm 两个凸多边形的交集

Algorithm 两个凸多边形的交集,algorithm,graphics,polygon,Algorithm,Graphics,Polygon,我有两个凸多边形。多边形实现为其顶点的循环列表。如何找到这两个多边形的交点 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 polyg

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

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)


以下是一些链接:

  • (pdf)

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

在两个多边形中查找最上面的点。为简单起见,假设没有水平边。创建多边形左右边界的边列表

沿平面向下扫掠时,存储4条边<代码>左边缘C1,
右边缘C1
左边缘C2
右边缘C2
。您不需要任何复杂的渲染边,因为只有四条边。只需迭代所有可能的选项,就可以找到下一个事件点

在每个事件点,一些边出现在其交点的边界上。基本上,在每个事件点,您可以有三种情况中的一种(见图)


除了@Yola漂亮的飞机扫描描述, 中介绍了一种线性时间算法
,第7章。C&Java代码可用(在同一链接)。有几种复杂的退化情况,例如,当两个多边形在一个点相交,或者相交是一段时。

在情况3中,您确定必须将V3添加到相交处吗?这似乎不正确。我重写它是为了更好地与Sutherland-Hudgman算法保持一致。