Minimum distance between two polygons

86 views Asked by At

I have worked on implementing the algorithm required for detecting minimum distance between two convex polygons P and Q, but I reached only detecting the distance between vertices, how can I detect the minimum distance when on edges of the polygons.

What is needed exactly, what are native java can do with edges to find the minimum distance? Is there direct native Java methods calculate the points on the edges and how to use within my code piece below?

        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

There are 0 answers