java 平面上最近两个点之间的距离

时间:2022-04-21 21:50:10
public class ClosestPair{
public static void main(String[] args) {
float[][] a = new float[][]{{3, 3}, {1, 5}, {4, 6}, {2, 8}, {9, 9},{2, 1}, {7, 2}, {6, 5}, {9, 4}, {5, 9}};
float d = (float) 1e9;
for(int i=0; i<10; i++){
for(int j=i+1; j<10; j++){
float dx = a[i][0] - a[j][0];
float dy = a[i][1] - a[j][1];
d = min(d, Math.sqrt(dx*dx+dy*dy));
}
}
System.out.println(d);
}
private static float min(float d, double sqrt) {
return d = d>sqrt?d=(float) sqrt:d;
}
}