我如何编写一个函数来在 Java 的二维数组中找到 2 个最近的点?

How can I write a function to find 2 closest points in 2D dimensional array in Java?

如何在 Java 的数组中找到 2 个最近的点?

我的输入是,

int[][] tour = {{0, 0}, {4, 0}, {4, 3}, {0, 3}};

static double calcDistanceBetweenWaypoints(int[] src, int[] dest) {
        assert src.length == 2;
        assert dest.length == 2;
        //(0,0),(0,4)
        return Math.sqrt((dest[0] - src[0])*(dest[0] - src[0]) + (dest[1] - src[1])*(dest[1] - src[1]));
    }

   public static int[][] getClosestWaypoints(int[][] tour) {
       throw new UnsupportedOperationException("Not supported yet.");
       //I dont know how to do with this function.
   }

我的输出是,

closest waypoints    : [(4/0) -> (4/3)], distance: 3,00

另一个解决方案:

public static int[][] getClosestWaypoints(int[][] tour) {
    double minDist = Integer.MAX_VALUE;
    int[] pt1 = null;
    int[] pt2 = null;
    for (int i = 0; i < tour.length-1; ++i) {
        for (int j = i+1; j < tour.length; ++j) {
            double dist = calcDistanceBetweenWaypoints(tour[i], tour[j]);
            if (dist < minDist) {
                minDist = dist;
                pt1 = tour[i];
                pt2 = tour[j];
            }
        }
    }
    return new int[][] {pt1, pt2};
}