两个多边形之间的最小距离

时间:2018-09-21 21:32:29

标签: java convex-polygon

我已经致力于实现检测两个凸多边形P和Q之间的最小距离所需的算法,但是我只检测顶点之间的距离,当在多边形的边缘上时如何检测最小距离。

确切需要什么,本机Java可以对边缘进行什么操作以找到最小距离? 是否有直接的本机Java方法计算边缘点以及如何在下面的代码段中使用它们?

        static double solve(List<List<Integer>> p, List<List<Integer>> q) {
//        confirmPositiveArea(p);
//        confirmPositiveArea(q);
        double minLength = 100000000;
        for (List<Integer> qq : q) {
            for (List<Integer> pp : p) {
                double current = distanceBTWPoints(pp, qq);
                if (current < minLength) {
                    minLength = current;
                }
            }
        }
        System.out.println(minLength);
        return minLength;
    }

    static double distanceBTWPoints(List<Integer> p, List<Integer> q) {
        double xDiff = q.get(0) - p.get(0);
        double yDiff = q.get(1) - p.get(1);
//        if(xDiff==0 || yDiff==0)
//            return 0;
        System.out.println(+p.get(0) + "  " + q.get(0) + "  " + p.get(1) + "  " + q.get(1) + " x:" + xDiff + "  y: " + yDiff + " p: " + ((xDiff * xDiff) + (yDiff * yDiff)) + " VAl= " + Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
//        System.out.println("p.get(0): "+p.get(0) +"  q.get(0) :"+q.get(0) +"  p.get(1):  "+p.get(1) +"  q.get(1): "+q.get(1) +" xDiff: " + xDiff + "  yDiff: " + yDiff + " plus: "+((xDiff * xDiff) + (yDiff * yDiff))+ " VAl= " + Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));

        return Math.hypot(xDiff, yDiff);
    }

public static void main(String[] args) {

    List<List<Integer>> p = new ArrayList<>();
    List<List<Integer>> q = new ArrayList<>();
    List<Integer> list1 = new ArrayList<>();
    list1.add(2);
    list1.add(1);
    p.add(list1);
    list1 = new ArrayList<>();
    list1.add(10);
    list1.add(1);
    p.add(list1);
    list1 = new ArrayList<>();
    list1.add(10);
    list1.add(5);
    p.add(list1);
    list1 = new ArrayList<>();
    list1.add(2);
    list1.add(5);
    p.add(list1);
    List<Integer> list2 = new ArrayList<>();
    list2.add(15);
    list2.add(10);
    q.add(list2);
    list2 = new ArrayList<>();
    list2.add(20);
    list2.add(10);
    q.add(list2);
    list2 = new ArrayList<>();
    list2.add(20);
    list2.add(15);
    q.add(list2);
    list2 = new ArrayList<>();
    list2.add(15);
    list2.add(15);
    q.add(list2);
    System.out.println(solve(p, q));
}

0 个答案:

没有答案