下面的代码部分解决了这个问题,但仍然需要通过检测更靠近另一个多边形的边缘上的点来增强,因为这部分只适用于顶点
static double solve(List<List<Integer>> p, List<List<Integer>> 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)));
System.out.println("--" + Math.hypot(xDiff, yDiff));
System.out.println("--" + Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
return Math.hypot(xDiff, yDiff);
}