计算两点之间的距离(二维、三维)

该博客介绍了如何在2D和3D空间中计算多个点之间的欧氏距离。提供了坐标实体类Point以及分别用于计算2D和3D点之间距离的静态方法。这些方法通过遍历坐标列表并应用毕达哥拉斯定理来累加各段距离,从而得到所有点之间的总距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景:

参考:两点之间距离

计算两点之间的距离

空间两点间距离
欧氏距离( Euclidean distance)也称欧几里得距离,它是一个通常采用的距离定义,它是在m维空间中两个点之间的真实距离。

在这里插入图片描述


解决方案:

坐标实体类

public class Point {
    public float x;
    public float y;
    public float z;

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

计算方式

2D

/**
     * 计算所有2d点之间的距离.
     *
     * @param coordinates 所有坐标点.
     * @return 返回所有坐标点距离的值
     */
    public static double calculatePointsDistance(List<Point> coordinates) {
        if (coordinates.size() < 2) {
            return 0.0;
        }

        if (coordinates.size() == 2) {
            double powX = Math.pow((coordinates.get(0).x - coordinates.get(1).x), 2);
            double powY = Math.pow((coordinates.get(0).y - coordinates.get(1).y), 2);
            return Math.sqrt(powX + powY);
        }

        double distance = Math.sqrt(Math.pow((coordinates.get(0).x - coordinates.get(coordinates.size() - 1).x), 2)
                + Math.pow((coordinates.get(0).y - coordinates.get(coordinates.size() - 1).y), 2));
        for (int i = 0; i < coordinates.size() - 1; i++) {
            double powX = Math.pow((coordinates.get(i).x - coordinates.get(i + 1).x), 2);
            double powY = Math.pow((coordinates.get(i).y - coordinates.get(i + 1).y), 2);
            distance += Math.sqrt(powX + powY);
        }
        return distance;
    }

3D

/**
     * 计算所有3d点之间的距离.
     *
     * @param coordinates 所有坐标点.
     * @return 返回所有坐标点距离的值
     */
    public static double calculateTdPointDistance(List<Point> coordinates) {
        if (coordinates.size() < 2) {
            return 0.0;
        }
        if (coordinates.size() == 2) {
            double powX = Math.pow((coordinates.get(0).x - coordinates.get(1).x), 2);
            double powY = Math.pow((coordinates.get(0).y - coordinates.get(1).y), 2);
            double powZ = Math.pow((coordinates.get(0).z - coordinates.get(1).z), 2);
            return Math.sqrt(powX + powY + powZ);
        }
        double distance = Math.sqrt(Math.pow((coordinates.get(0).x - coordinates.get(coordinates.size() - 1).x), 2)
                + Math.pow((coordinates.get(0).y - coordinates.get(coordinates.size() - 1).y), 2)
                + Math.pow((coordinates.get(0).z - coordinates.get(coordinates.size() - 1).z), 2));
        for (int i = 0; i < coordinates.size() - 1; i++) {
            double powX = Math.pow((coordinates.get(i).x - coordinates.get(i + 1).x), 2);
            double powY = Math.pow((coordinates.get(i).y - coordinates.get(i + 1).y), 2);
            double powZ = Math.pow((coordinates.get(i).z - coordinates.get(i + 1).z), 2);
            distance += Math.sqrt(powX + powY + powZ);
        }
        return distance;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃骨头不吐股骨头皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值