可以使用以下代码来计算两个点之间的距离:

public class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double distanceTo(Point otherPoint) {
        double dx = this.x - otherPoint.x;
        double dy = this.y - otherPoint.y;
        return Math.sqrt(dx * dx + dy * dy);
    }

    public static void main(String[] args) {
        Point point1 = new Point(1, 2);
        Point point2 = new Point(4, 6);
        
        double distance = point1.distanceTo(point2);
        System.out.println("两点之间的距离为:" + distance);
    }
}

以上代码定义了一个 Point 类,包含了 x 和 y 坐标的属性,以及计算两点之间距离的方法 distanceTo。在 main 方法中,创建了两个点对象,然后调用 distanceTo 方法来计算两点之间的距离,并打印出结果

java 求2个点之间的距离

原文地址: http://www.cveoy.top/t/topic/h3Uo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录