Voronoi图缺少多边形

时间:2017-07-21 18:15:34

标签: java polygon voronoi

我的问题很奇怪,在寻找答案时我没有看到类似的东西。我已经从Delaunay三角剖分中制作了一个Voronoi图,并且大部分时间它工作正常,但偶尔也会在图中间缺少一个随机多边形。这是我在三角测量中的Voronoi图的代码。

public void gon() {
    for(Node n : nodes) { // step through all the nodes which make up the triangulation
        ArrayList<Triangle> centers = new ArrayList<Triangle>();
        for(Triangle t:tri) {
            if((t.ax == n.x && t.ay == n.y)||(t.bx == n.x && t.by == n.y)||(t.cx == n.x && t.cy == n.y)) {
                centers.add(t);
            }
        }
        if(centers.size() != 0) { // as long as there are triangles in the list that will make up a polygon
            ArrayList<Triangle> orderPoly = order(centers);

            int[] x = new int[orderPoly.size()];
            int[] y = new int[orderPoly.size()];

            Cell cell = new Cell(orderPoly.size());

            orderPoly = order(centers);

            for(int i = 0; i < orderPoly.size(); i++) {
                x[i] = orderPoly.get(i).circumCenter.x;
                y[i] = orderPoly.get(i).circumCenter.y;
            }

            cell.x=x;
            cell.y=y;

            cell.check();

            cells.add(cell);
        }
    } 
}

这里的代码遍历构成三角剖分的点,获取使用该点的所有三角形,对点(下面的代码)进行排序,然后继续将它们添加到Voronoi单元类,其中包含用于绘制的多边形目的,然后将该细胞添加到细胞Arraylist。

public ArrayList<Triangle> order(ArrayList<Triangle> centers){

    ArrayList<Triangle> orderPoly = new ArrayList<Triangle>();

    orderPoly.add(centers.get(0));

    Triangle temp = orderPoly.get(0);

    for(Triangle t : centers) {
        for(Triangle p : centers) {
            if(shareSide(p,temp) && !orderPoly.contains(p)) {
                orderPoly.add(p);
                temp = p;
                break;
            }
        }
    }
    return orderPoly;
}

这适用于绝大多数细胞,但偶尔也会缺少细胞。我相信单元格存在,但是因为多边形自相交而没有被绘制。我已经使用种子随机生成器进行了大量调试,这似乎是问题所在。这让我相信我的细胞生成必定存在缺陷。有人有什么建议或看错了吗?

如果有人需要,我可以为我的任何课程发布更多代码。

0 个答案:

没有答案
相关问题