美文网首页
[小练习] 求点到原点距离平方

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

作者: 大写K | 来源:发表于2019-10-11 17:31 被阅读0次
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;
    }
}

相关文章

网友评论

      本文标题:[小练习] 求点到原点距离平方

      本文链接:https://www.haomeiwen.com/subject/spdxmctx.html