Java commons-math问题 形成和提取ConvexHull2D的顶点。

问题描述 投票:0回答:1

我正在研究ConvexHull问题,我需要识别一个凸壳的顶点。我正在使用Apache commons-math的-ConvexHull2D。以下是我目前所掌握的情况

public static void main(String[] args) {

        Vector2D v1 = new Vector2D(1, 3);
        Vector2D v2 = new Vector2D(-1, 3);
        Vector2D v3 = new Vector2D(0, -2);
        Vector2D v4 = new Vector2D(-1, -3);
        Vector2D v5 = new Vector2D(-12, -13);
        Vector2D v6 = new Vector2D(-10, -30);

        Vector2D[] vertices = {v1,v2,v3,v4,v5,v6};
        double tolerance = 1;

        ConvexHull2D ch = new ConvexHull2D(vertices, tolerance);
        vertices =ch.getVertices();

        System.out.println(vertices);

    }

但在这段代码中,我看到了这个异常

Exception in thread "main" org.apache.commons.math3.exception.MathIllegalArgumentException: vertices do not form a convex hull in CCW winding
    at org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHull2D.<init>(ConvexHull2D.java:69)
    at run_ootb_templates.LongitudeLatitudeTest.main(LongitudeLatitudeTest.java:22)

我对凸壳的理解是空间中的任何3个点都能够形成一个凸壳,除了这3个点之外的任何其他点都可以容纳在这个凸壳的边界中。

任何帮助对解决感谢,也如果任何样本数据集,这样我就可以理解这个。

java machine-learning geometry convex-hull apache-commons-math
1个回答
1
投票

关于Apache commons-math的这一领域的文档似乎不是很好,但从看了一下 源码 可见 ConvexHull2D 要求顶点已经形成一个凸多边形。这是通过检查沿边界移动时是否有一致的转动方向来实现的。你的点没有形成一个凸多边形,因此出现了异常。

相反的,你想要的是使用一个实现 凹凸壳生成器2D 接口,例如 MonotoneChain,以建立一个 ConvexHull2D 经由 generate(Collection<Vector2D> points) 方法。

© www.soinside.com 2019 - 2024. All rights reserved.