Java中求解大于给定半径的最小距离坐标
在日常开发中,我们经常会遇到坐标距离计算相关的需求,比如筛选出距离某个中心点超过指定半径的坐标点,再从这些点中找到距离最小的目标。本文将详细介绍如何使用Java实现这个逻辑,包含完整的代码实现和逻辑说明。
问题场景说明
假设我们有一个中心坐标(centerX, centerY),以及一组待筛选的坐标点集合points,每个坐标点包含x轴和y轴的坐标值。现在需要完成两个步骤:
- 第一步:筛选出所有与中心坐标的欧氏距离大于给定半径
radius的坐标点 - 第二步:从筛选出的点中,找到距离中心坐标最小的点,如果存在多个距离相同的点,返回第一个出现的即可
如果不存在符合条件的坐标点,返回null即可。
核心逻辑实现
首先我们需要定义一个坐标点的实体类,用来存储每个点的x、y坐标,以及计算该点到中心点的距离。距离计算使用欧氏距离公式:distance = sqrt((x1-x2)² + (y1-y2)²),不过如果是比较距离大小,我们可以去掉开方运算,用距离的平方来比较,这样可以减少计算开销。
坐标点实体类定义
我们先定义Point类,包含坐标属性和距离计算方法:
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// 计算当前点到目标点的距离平方,避免开方减少计算量
public double distanceSquareTo(double targetX, double targetY) {
double dx = this.x - targetX;
double dy = this.y - targetY;
return dx * dx + dy * dy;
}
// getter方法
public double getX() {
return x;
}
public double getY() {
return y;
}
@Override
public String toString() {
return "Point{x=" + x + ", y=" + y + "}";
}
}核心筛选逻辑实现
接下来实现核心的筛选方法,逻辑分为三步:先校验入参合法性,再遍历所有点筛选符合距离要求的点,最后从筛选结果中找到距离最小的那个。
import java.util.ArrayList;
import java.util.List;
public class CoordinateFinder {
/**
* 查找大于给定半径的最小距离坐标
* @param centerX 中心x坐标
* @param centerY 中心y坐标
* @param radius 给定半径
* @param points 待筛选的坐标点集合
* @return 符合条件的最小距离坐标,不存在则返回null
*/
public static Point findMinDistancePointOverRadius(double centerX, double centerY, double radius, List<Point> points) {
// 入参校验
if (points == null || points.isEmpty()) {
return null;
}
if (radius < 0) {
// 半径为负数时,所有点都符合条件,直接找距离最小的点即可
return findMinDistancePoint(centerX, centerY, points);
}
// 第一步:筛选出距离大于半径的点
List<Point> validPoints = new ArrayList<>();
double radiusSquare = radius * radius; // 半径的平方,用于比较
for (Point point : points) {
double distanceSquare = point.distanceSquareTo(centerX, centerY);
if (distanceSquare > radiusSquare) {
validPoints.add(point);
}
}
// 没有符合条件的点,返回null
if (validPoints.isEmpty()) {
return null;
}
// 第二步:从符合条件的点中找到距离最小的点
return findMinDistancePoint(centerX, centerY, validPoints);
}
/**
* 从点集合中找到距离目标点最小的点
*/
private static Point findMinDistancePoint(double targetX, double targetY, List<Point> points) {
if (points == null || points.isEmpty()) {
return null;
}
Point minPoint = points.get(0);
double minDistanceSquare = minPoint.distanceSquareTo(targetX, targetY);
for (int i = 1; i < points.size(); i++) {
Point current = points.get(i);
double currentDistanceSquare = current.distanceSquareTo(targetX, targetY);
if (currentDistanceSquare < minDistanceSquare) {
minDistanceSquare = currentDistanceSquare;
minPoint = current;
}
}
return minPoint;
}
// 测试方法
public static void main(String[] args) {
// 构造测试数据:中心坐标(0,0),半径3
double centerX = 0;
double centerY = 0;
double radius = 3;
List<Point> points = new ArrayList<>();
points.add(new Point(1, 1)); // 距离sqrt(2)≈1.414,小于3,不符合
points.add(new Point(2, 2)); // 距离sqrt(8)≈2.828,小于3,不符合
points.add(new Point(4, 0)); // 距离4,大于3,符合
points.add(new Point(3.5, 0)); // 距离3.5,大于3,符合
points.add(new Point(3.5, 0)); // 距离3.5,大于3,符合,和上一个距离相同
points.add(new Point(5, 0)); // 距离5,大于3,符合
Point result = findMinDistancePointOverRadius(centerX, centerY, radius, points);
if (result != null) {
System.out.println("符合条件的最小距离坐标为:" + result);
System.out.println("该点到中心的距离约为:" + Math.sqrt(result.distanceSquareTo(centerX, centerY)));
} else {
System.out.println("没有符合条件的坐标点");
}
}
}代码逻辑说明
上面的实现中,我们做了几个优化点:
- 距离比较时使用距离的平方而不是开方后的距离,避免了耗时的开方运算,提升计算效率,尤其是在点数量很多的时候效果更明显
- 单独抽取了查找最小距离点的方法,避免代码重复,也方便后续扩展
- 入参做了合法性校验,处理了点集合为空、半径为负数等边界情况
测试代码的运行结果会输出符合条件的最小距离坐标为:Point{x=3.5, y=0},因为它的距离是3.5,是所有大于半径3的点中最小的,即使后面还有一个相同的3.5距离的点,也会返回第一个出现的。
边界情况说明
实际使用中还需要注意几个边界场景:
- 如果半径等于0,那么所有不等于中心点的坐标都会被筛选出来,最后返回距离中心最近的非中心点
- 如果所有点到中心的距离都小于等于半径,那么方法会返回null,调用方需要处理这个返回值
- 如果坐标点包含null元素,上面的代码会抛出空指针异常,实际使用中可以在遍历时增加非空判断,或者在方法入口处校验集合元素
Java坐标距离计算欧氏距离半径筛选最小距离点几何算法修改时间:2026-05-24 12:40:28