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

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

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void setZ(double z) {
        this.z = z;
    }

    public double distanceToOrigin() {
        return Math.sqrt(x * x + y * y + z * z);
    }
}

public class PointTest {
    public static void main(String[] args) {
        Point point = new Point(3, 4, 5);
        System.out.println('Distance to origin: ' + point.distanceToOrigin());
    }
}

运行结果: Distance to origin: 7.0710678118654755

Java 三维空间点类实现及距离计算示例

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

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