📄 508fbbfce865001b1748b1d06439a30d
字号:
package distance;
public class ShortestDistance {
public static int selectPoint1;
public static int selectPoint2;
public static double minDistance ;
/*all of the array start from 0*/
public void findShortestDistance(Point[] PointSet,int start,int end){
System.out.println("hi");
if(start < end){
int BreakPoint = (start + end)/2;
findShortestDistance(PointSet, start, BreakPoint);
System.out.println("aa");
findShortestDistance(PointSet, BreakPoint + 1, end);
System.out.println("dd");
Shortest(PointSet, start, BreakPoint, end);
System.out.println("hh");
}
}
public void Shortest(Point[] PointSet,int start,int breakPoint ,int end){
System.out.println("cc");
int lengthLeft = breakPoint - start + 1;
int lengthRight = end - breakPoint;
Point[] arrayLeft = new Point[lengthLeft];
Point[] arrayRight = new Point[lengthRight];
for(int i = 0; i < lengthLeft; i++){
arrayLeft[i] = PointSet[start + i];
}
for(int i = 0; i < lengthRight; i++){
arrayRight[i] = PointSet[breakPoint + i + 1];
}
ShortestDistance.minDistance = 100.0;
for(int i = 0; i < arrayLeft.length; i++){
for(int j = 0; j < arrayRight.length; j++){
double currentDistance = ShortestDistance.distanceOfTwoPoint(arrayLeft[i],arrayRight[j]);
System.out.println("currentDistance" + currentDistance);
if(currentDistance < ShortestDistance.minDistance){
ShortestDistance.minDistance = currentDistance;
ShortestDistance.selectPoint1 = i;
ShortestDistance.selectPoint2 = j;
System.out.println("distance:" + minDistance);
System.out.println("selectPoint1:" + selectPoint1);
System.out.println("selectPoint2:" + selectPoint2);
}
}
}
}
public static double distanceOfTwoPoint(Point pointOne,Point pointTwo){
double x1 = pointOne.getXCoordinate();
double y1 = pointOne.getYCoordinate();
double x2 = pointTwo.getXCoordinate();
double y2 = pointTwo.getYCoordinate();
return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -