在LibGDX中正确使用多边形三角剖分

时间:2015-06-24 08:50:55

标签: java libgdx triangulation polygons

我使用以下代码对Voronoi区域进行三角测量:

EarClippingTriangulator triangulator = new EarClippingTriangulator();
ConvexHull hull = new ConvexHull();

final FloatArray rawPoints = voronoiArea.getPoints(factor, factor); // This will get a set of points in no particular order
final FloatArray hullArray = hull.computePolygon(rawPoints, false); // To avoid concave polygons
final Polygon polygon = new Polygon(hullArray.toArray());
final PolygonRegion region = new PolygonRegion(texture, polygon.getVertices(),
                    triangulator.computeTriangles(polygon.getVertices()).toArray());
final PolygonSprite sprite = new PolygonSprite(region);

这在95%的情况下都可以正常工作,但偶尔我会得到无效的多边形 - 渲染时它们显示为有1-2个三角形“丢失”的正多边形。

还有其他人有这个问题吗? 任何人都可以发现此代码存在问题吗?

编辑:结果图片:

The green triangle is the problem...

编辑:

我已经做了一些测试 - 我现在有一个用10个顶点定义的单个多边形,并且多边形仍然呈现错误。

代码:

      points = new float[] {
                34.983868f, 31.306454f, 27.999998f, 40.285713f,
                28.0f, 40.285713f, 28.0f, 54.0f,
                34.98387f, 31.306452f, 54.0f, 32.76923f,
                28.0f, 54.0f, 54.0f, 54.0f,
                54.0f, 54.0f, 54.0f, 32.76923f
        };

        net.dermetfan.utils.math.GeometryUtils.arrangeConvexPolygon(points, true);

        for (int i = 0; i < points.length; i++) {
            points[i]*=10f;
        }

        final Polygon p = new Polygon(points);
        final float posX = voronoiArea.getCenter().x;
        final float posY = voronoiArea.getCenter().y;
        p.setOrigin(posX, posY);

        final short[] triangles = triangulator.computeTriangles(p.getVertices()).toArray();
        final PolygonRegion region = new PolygonRegion(fill[voronoiArea.site.getSiteNum() % 3], p.getVertices(), triangles);

现在看一下,我可以看到有几个彼此非常接近的顶点 - 例如27.999998f和28.0f - 这让我怀疑整个问题是因为某些舍入错误... < / p>

0 个答案:

没有答案