类型增强
Pair
在
/** 获取最近点和距离 */
public static Pair<Point, Double> getNearestPointAndDistance(Point point, Point[] points) {
// 检查点数组为空
if (ArrayUtils.isEmpty(points)) {
return null;
}
// 获取最近点和距离
Point nearestPoint = points[0];
double nearestDistance = getDistance(point, points[0]);
for (int i = 1; i < points.length; i++) {
double distance = getDistance(point, point[i]);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestPoint = point[i];
}
}
// 返回最近点和距离
return Pair.of(nearestPoint, nearestDistance);
}
函数使用案例如下:
Point point = ...;
Point[] points = ...;
Pair<Point, Double> pair = getNearestPointAndDistance(point, points);
if (Objects.nonNull(pair)) {
Point point = pair.getLeft();
Double distance = pair.getRight();
...
}