【问题标题】:Connected vertices from polygon, how to get the new polygons多边形的连接顶点,如何获得新的多边形
【发布时间】:2017-07-19 14:56:50
【问题描述】:

我有一个多边形,其中一些顶点与线相连(起点和终点是多边形的一个顶点)。每条线(连接顶点)有 4 条规则:

  • 该线不与其他线相交
  • 线不与多边形相交
  • 线完全包含在多边形中
  • 线不是多边形的边

示例: 在图像中,红线是坏线, 黑线是多边形边缘,绿线是好线。

  • h
  • 线 i 不好,因为它与多边形相交
  • 线j 不好,因为它与线hi 相交
  • 线k 不好,因为它是多边形的一条边
  • 线g 不好,因为它不包含在多边形中

我有一个包含多边形顶点的数组,以及一个包含线条的数组,如下所示:

polygon = [
  {x: ..., y: ...},
  ...
]
lines = [
  {
    p1: {x: ..., y: ...},
    p2: {x: ..., y: ...}
  },
  ...
]

lines 数组仅包含有效行。

如何获得被线切割的多边形。

我想要这样的东西:

function getSlicedPolygons(polygon, lines){
  // No check for valid lines, they are already valid
  // Do the algorithm...
}

到目前为止我已经尝试过什么

我从第一个顶点开始,然后继续,直到我到达一个连接的顶点。从那个顶点,我去到在线的另一端连接的顶点。现在我下一步直到另一个连接的顶点,依此类推,直到我到达我开始的顶点。现在我有了第一个多边形。我找不到其他人...

代码(实现,不是真正的代码):

function getSlicedPolygons(polygon, line){
    var results = []
    var ofl = 0; // Overflow counter, to prevent infinite looping
    while(lines.length > 0){
        // Iterate through polygon
        var i = 0;
        var r = []; // Array of new indices
        var iterations = 0; // An overflow counter again
        while(i < polygon.length){
            r.push[i]
            // findNextConnectionIndex(...) searches for a line
            // that connects with this vertex and returns the index of
            // the other connected vertex
            var n = findNextConnectionIndex(i, polygon, lines) || i+1
            i=n;
            // Don't loop infinite
            iterations++;
            if(iterations > 10) break;
        }
        var result = [];
        for(var z = 0; z<r.length; z++){
            result.push(polygon[r[z]])
        }
        results.push(result)
        // Now I should do something to get another polygon next 
        // time ...
        // Don't loop infinite
        ofl++;
        if(ofl >= 10) break;
    }
    return results;
}

它在一个数组中返回相同的多边形 10 次...

【问题讨论】:

  • 我试图从第一个顶点开始并获取下一个顶点,直到我到达一个行尾,然后我移动到另一个行尾并继续下一个,直到我回到第一个顶点。现在我有第一个,但没有其他的。我会编辑我的问题...
  • 我相信@JonasGiuro 是在问你到目前为止尝试过什么代码。我们希望看到您实际上是在尝试解决这个问题,而不是将您的工作交给 SO 上的志愿者。
  • 哦,对不起,我会再次编辑它...
  • 顺序是否重要,即如果两条线相交,那么第一条是好的,第二条是坏的?此外,如果 j 和 h 被交换,那是否会使 h 变坏(即与以前被认为是坏的线相交是否重要)?
  • 顺序无关紧要。示例:连接2个顶点,检查是否有效,然后连接另外2个顶点检查是否有效,以此类推...

标签: javascript algorithm polygon


【解决方案1】:

将多边形及其相交线视为具有循环的无向图,并对其应用循环检测算法。既然我们知道了连接线,事情就变得简单了一点,我们实际上可以在O(V)中解决问题。

这将是一个足以解释基本原理的模型。我们可以将多边形转换为一个由线条列表分割的矩形。由于没有线可以相交,因此这也适用于生成的矩形。现在可以从图的一个角落开始,沿着两条边移动,直到在两条路径上都达到 3 度的顶点。因此,我们找到了第一个多边形,它是对原始多边形进行切片的结果。从上一步中到达的两个点继续,直到再次到达 3 度的顶点。当两条路径相遇并且您已列出所有可能的多边形时,终止此步骤。

运行此过程的单个步骤的图表:

寻找“角落”顶点

从图形/多边形中的任意点开始,沿多边形沿任意方向遍历顶点,直到达到 3 阶顶点。存储相应的切片线并沿多边形继续,直到达到 3 度的顶点。如果是同一条切片线,则您已找到“角”顶点,否则存储新的切片线并重复。

编辑

python 中的工作实现:

def slice_polygon(poly, part):
    # find the "corner point"
    last_slice = None
    last_pt = None

    for pt in poly:
        s = [x for x in part if pt in x]

        if s:
            if last_slice in s:
                break

            last_slice = s[0]

        last_pt = pt

    # find slicing starting from border-point
    a = poly.index(last_pt)
    b = (a + 1) % len(poly) # current positions on the polygon
    sliced_poly = []    # current polygon
    slicing = []    # list of all polygons that are created by the slicing
    while a != b:
        if not [x for x in part if poly[a] in x]:
            # point doesn't lie on slicing-line => add to current polygon
            sliced_poly.insert(0, poly[a])             # prepend point
            a = (a - 1 + len(poly)) % len(poly)  # advance a by one
        elif not [x for x in part if poly[b] in x]:
            # point doesn't lie on slicing-line => add to current polygon
            sliced_poly.append(poly[b])                # append point
            b = (b + 1 + len(poly)) % len(poly)  # advance by one
        else:
            # append points of slicing line
            sliced_poly.insert(0, poly[a])
            sliced_poly.append(poly[b])

            # store created polygon and start over
            slicing.append(sliced_poly)
            sliced_poly = []

            # remove partitioning-line at which the algorithm stopped
            part.remove([x for x in part if poly[a] in x and poly[b] in x][0])

    # add last point to the current polygon, as it's not yet added to it
    sliced_poly.append(poly[a])
    # add last polygon to result-set
    slicing.append(sliced_poly)

    return slicing


# test
polygon = [(150, 110), (270, 40), (425, 90), (560, 150), (465, 290), (250, 290), (90, 220)]
partition = [((270, 40), (250, 290)), ((425, 90), (250, 290))]
print(slice_polygon(polygon, partition))

输出:

[[(425, 90), (560, 150), (465, 290), (250, 290)], [(270, 40), (425, 90), (250, 290)], [(90, 220), (150, 110), (270, 40), (250, 290)]]

输入:

由于总共有两个“角点”(至少),如果我们遍历多边形一次,我们可以保证至少找到一个。

【讨论】:

  • 感谢您的回答!
  • @OlafNankman 有时间我会用 python 版本更新 ;)
  • @OlafNankman 更新了一个有效的 python 解决方案。我做了一个简单的测试,但如果我错过了什么,请随时通知我。
猜你喜欢
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多