是否有一种算法可以分离出共享一条边的多边形?

时间:2019-09-06 19:59:22

标签: algorithm geometry polygon traversal

我有一个定义多边形的顶点列表,一个连接这些顶点并定义多边形轮廓的外围边缘列表以及一个连接顶点的内部边缘列表,有效地分割了多边形。两条边都不相交(它们仅在起点和终点相交)。

我想通过在内部边缘将较大的多边形分割为较小的部分。基本上,我需要知道哪些顶点集是没有相交边的多边形的一部分。

基本上,这是我所拥有的信息:

enter image description here

顶点[0、1、3、5、7、9、11、13、15]定义了我的多边形的外周边,蓝色边缘(5、13)是将该周边分成两个多边形的内边缘。 (忽略水平的紫色线,它们是多边形梯形化以找到边(5、13)的结果。它们没有进一步的含义。)

这就是我想要得到的:

enter image description here

一个多边形由顶点[0、1、3、5、13、15]定义,另一个由[5、7、9、11、13]定义。

我需要一种适用于任意数量的内部分割边缘的解决方案。 最后,我希望能够对多边形进行如下划分:

enter image description here

子多边形不一定是凸的。对于任何依赖于它的算法,这可能都很重要。红色子多边形具有凹形顶点(13)。

最初,我的想法是沿顺时针或逆时针方向遍历每个子多边形的内部,跟踪我遇到的顶点及其顺序。但是,在查找起始边/顶点并确保下一个cw或ccw点实际上位于我要提取的子多边形的内部时,我遇到了麻烦。

我尝试用Google搜索解决方案,但这是我不熟悉的数学领域,不知道要搜索什么。如何解决这个问题的想法/算法将不胜感激! (我不需要任何实际的代码,只需对如何执行此操作或伪代码进行说明就可以了)

现在,不幸的是,我没有要显示的代码,因为我需要先尝试的概念。我不知道该如何解决这个问题,因此无法编写任何可以完成我需要做的事情的代码。

编辑:

这只是我最终要做的一步,即多边形三角剖分。我已经阅读了许多有关该问题的解决方案,并希望通过梯形化来实现它,以获得单调多边形并最终将其三角剖分。这基本上是梯形化的最后一步(或者我猜想是下一步),这在我所能找到的有关该主题的任何资源中都没有得到解释。

梯形化的最终结果是内部边缘,这些内部边缘定义了分割为多个多边形(在我的情况下为垂直单调)。我只需要沿“数据结构”沿这些边缘分割多边形,以便可以单独处理子多边形。我希望这有助于澄清问题。

1 个答案:

答案 0 :(得分:0)

您需要的算法的关键是要知道如何对边缘进行排序:

找出沿(逆时针)顺序的下一条边

您可以使用以下公式计算从节点 i 到节点 j 的一条边的绝对角度:

atan2(jy-iy, jx-ix)

请参见atan2

(i,j)(j,k)之间的相对角度如下:

atan2(ky-jy, kx-jx) - atan2(jy-iy, jx-ix)

此表达式可能会产生[-?,?]范围之外的角度,因此您应通过加或减2?将结果映射回该范围。

因此,当您遍历了一条边(i,j)并且需要选择下一条边(j,k)时,您可以选择具有以下特征的边最小相对角度。

算法

分区算法实际上并不需要预先知道哪些边缘是内部边缘,因此我假设您只有边缘列表。该算法可能如下所示:

  1. 创建一个邻接表,以便为每个顶点提供一个相邻顶点的列表。
    • 在两个方向上将每个边缘都添加到此邻接表中,因此实际上为每个原始边缘添加了两个有向边缘
  2. 从邻接表中选择有向边(i,j),并将其从此处删除。
  3. 定义一个新的多边形并将顶点 i 添加为其第一个顶点。
  4. 重复直到返回到正在构造的多边形中的第一个顶点:
    • 将顶点 j 添加到多边形
    • 在不是顶点 i 的顶点 j 的邻居中找到
    • k k ,并使用上述公式将相对角度最小化
    • 将此角度添加到总和
    • 从顶点 j 的邻居中删除此有向边,这样就永远不会再次访问它了
    • i = j,j = k
  5. 如果角度的总和为正(将为2?),则将多边形添加到“正”多边形列表中,否则(将为-2?)将其添加到替代列表中。
  6. 从第2步开始重复,直到邻接列表中不再有定向边为止。
  7. 最后,您将有两个多边形列表。一个列表将只有一个多边形。这将是原始的外部多边形,可以忽略。另一个列表将具有分区。

作为演示,这是可运行的JavaScript代码段中的一些代码。它使用您在问题中描绘的示例之一(但将对顶点进行连续编号),根据此算法查找分区,并通过对识别出的多边形着色来显示结果:

function partition(nodes, edges) {
    // Create an adjacency list
    let adj = [];
    for (let i = 0; i < nodes.length; i++) {
        adj[i] = []; // initialise the list for each node as an empty one
    }
    for (let i = 0; i < edges.length; i++) {
        let a = edges[i][0]; // Get the two nodes (a, b) that this edge connects
        let b = edges[i][1]; 
        adj[a].push(b); // Add as directed edge in both directions
        adj[b].push(a);
    }
    // Traverse the graph to identify polygons, until none are to be found
    let polygons = [[], []]; // two lists of polygons, one per "winding" (clockwise or ccw)
    let more = true;
    while (more) {
        more = false;
        for (let i = 0; i < nodes.length; i++) {
            if (adj[i].length) { // we have unvisited directed edge(s) here
                let start = i;
                let polygon = [i]; // collect the vertices on a new polygon
                let sumAngle = 0;
                // Take one neighbor out of this node's neighbor list and follow a path
                for (let j = adj[i].pop(), next; j !== start; i = j, j = next) {
                    polygon.push(j);
                    // Get coordinates of the current edge's end-points
                    let ix = nodes[i][0];
                    let iy = nodes[i][1];
                    let jx = nodes[j][0];
                    let jy = nodes[j][1];
                    let startAngle = Math.atan2(jy-iy, jx-ix);
                    // In the adjacency list of node j, find the next neighboring vertex in counterclockwise order
                    //   relative to node i where we came from.
                    let minAngle = 10; // Larger than any normalised angle
                    for (let neighborIndex = 0; neighborIndex < adj[j].length; neighborIndex++) {
                        let k = adj[j][neighborIndex];
                        if (k === i) continue; // ignore the reverse of the edge we came from
                        let kx = nodes[k][0];
                        let ky = nodes[k][1];
                        let relAngle = Math.atan2(ky-jy, kx-jx) - startAngle; // The "magic"
                        // Normalise the relative angle to the range [-PI, +PI)
                        if (relAngle < -Math.PI) relAngle += 2*Math.PI;
                        if (relAngle >=  Math.PI) relAngle -= 2*Math.PI;
                        if (relAngle < minAngle) { // this one comes earlier in counterclockwise order
                            minAngle = relAngle;
                            nextNeighborIndex = neighborIndex;
                        }
                    }
                    sumAngle += minAngle; // track the sum of all the angles in the polygon
                    next = adj[j][nextNeighborIndex];
                    // delete the chosen directed edge (so it cannot be visited again)
                    adj[j].splice(nextNeighborIndex, 1);
                }
                let winding = sumAngle > 0 ? 1 : 0; // sumAngle will be 2*PI or -2*PI. Clockwise or ccw.
                polygons[winding].push(polygon);
                more = true;
            }
        }
    }
    // return the largest list of polygons, so to exclude the whole polygon,
    //   which will be the only one with a winding that's different from all the others.
    return polygons[0].length > polygons[1].length ? polygons[0] : polygons[1];
}

// Sample input:
let nodes = [[59,25],[26,27],[9,59],[3,99],[30,114],[77,116],[89,102],[102,136],[105,154],[146,157],[181,151],[201,125],[194,83],[155,72],[174,47],[182,24],[153,6],[117,2],[89,9],[97,45]];
let internalEdges = [[6, 13], [13, 19], [19, 6]];
// Join outer edges with inner edges to an overall list of edges:
let edges = nodes.map((a, i) => [i, (i+1)%nodes.length]).concat(internalEdges);
// Apply algorithm
let polygons = partition(nodes, edges);
// Report on results
document.querySelector("div").innerHTML =
    "input polygon has these points, numbered 0..n<br>" + 
    JSON.stringify(nodes) + "<br>" +
    "resulting polygons, by vertex numbers<br>" +
    JSON.stringify(polygons)

// Graphics handling
let io = {
    ctx: document.querySelector("canvas").getContext("2d"),
    drawEdges(edges) {
        for (let [a, b] of edges) {
            this.ctx.moveTo(...a);
            this.ctx.lineTo(...b);
            this.ctx.stroke();
        }
    },
    colorPolygon(polygon, color) {
        this.ctx.beginPath();
        this.ctx.moveTo(...polygon[0]);
        for (let p of polygon.slice(1)) {
            this.ctx.lineTo(...p);
        }
        this.ctx.closePath();
        this.ctx.fillStyle = color;
        this.ctx.fill();
    }
};

// Display original graph
io.drawEdges(edges.map(([a,b]) => [nodes[a], nodes[b]]));
// Color the polygons that the algorithm identified
let colors = ["red", "blue", "silver", "purple", "green", "brown", "orange", "cyan"];
for (let polygon of polygons) {
    io.colorPolygon(polygon.map(i => nodes[i]), colors.pop());
}
<canvas width="400" height="180"></canvas>
<div></div>