[小练习] 求点到原点距离平方

2019-10-11  本文已影响0人  大写K
public class Test {
    public static void main(String[] args) {
        Point p1 = new Point(1.0, 2.0, 3.0);
        Point origin = new Point(0.0, 0.0, 0.0);
        System.out.println(p1.getDistance(origin));
    }
}

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 getDistance(Point p) {
        double distance = (this.x - p.x) * (this.x - p.x)
                        + (this.y - p.y) * (this.y - p.y)
                        + (this.z - p.z) * (this.z - p.z);
        return distance;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读