0

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));
}
Nesrin
  • 395
  • 3
  • 8
  • 1
    @AndreyTyukin, they're a relatively new user, and this is their first question, likely they did not post it that way intentionally.. Why not [Edit](https://stackoverflow.com/posts/52451283/edit) their question and show them how to properly post their code? – chickity china chinese chicken Sep 21 '18 at 21:35
  • I am asking on different implementation in Java, if there is a library generates the required distance directly without mathematics based algorithms – Nesrin Sep 21 '18 at 21:42
  • If you understand the algorithms explained in the other question, try implementing them. Have you tried that? – ubadub Sep 21 '18 at 21:44
  • Not the point of the question, but you can write your lists much more easily with `Arrays.asList`: `Arrays.asList(Arrays.asList(2, 1), Arrays.asList(10, 1) /* etc */)`. – Andy Turner Sep 21 '18 at 21:45
  • e.g.: https://github.com/simplegeo/jts/blob/master/src/com/vividsolutions/jts/operation/distance/DistanceOp.java – ubadub Sep 21 '18 at 21:45
  • Links are not opening in https://stackoverflow.com/questions/3700983/what-is-the-fastest-algorithm-to-calculate-the-minimum-distance-between-two-sets – Nesrin Sep 21 '18 at 21:50
  • Both are not native JDK based, they external libraries, I need algorithm to loop on edge points and compare the vertices sample in the main post https://stackoverflow.com/questions/2115976/geometry-library-for-java and https://github.com/simplegeo/jts/blob/master/src/com/vividsolutions/jts/operation/distance/DistanceOp.java – Nesrin Sep 21 '18 at 21:52
  • Links worked now, I think there is need for VPN, but I don't understand how to implement the algorithm – Nesrin Sep 21 '18 at 21:59
  • Dears, Why the site redirects me to another question?. I noted that the algorithm considered as a solution is not understandable and not also fulfilling the requirement I'm asking for. – Nesrin Sep 23 '18 at 01:29
  • I need to add a solution for this question in Java but number of characters not allowing me – Nesrin Nov 20 '18 at 03:33

0 Answers0