java 三维空间求三个点的坐标_用java语句写出三维空间的点,例子如下:麻烦帮我加上注释~...

共回答了18个问题采纳率:100%

public class Point {

private double x;

private double y;

private double z;

//无参数默认原点

public Point() {

this(0,0,0);

}

//构造方法指定坐标

public Point(double x,double y,double z) {

this.x = x;

this.y = y;

this.z = z;

}

public static void main(String args[]){

Point p = new Point(1,1,1);//构造,1,1,1点,距离远点距离是根号3,1.732.

System.out.println("Before new valued set,the point is:" + p.toString());

System.out.println("Before new valued set,the distance to (0,0,0) is:" + p.getDistance());

p.setPosition(2,2,2);//重新设置点坐标

System.out.println("After new valued set,the point is:" + p.toString());

System.out.println("After new valued set,the distance to (0,0,0) is:" + p.getDistance());

}

public void setX(double x) {//设置x坐标

this.x = x;

}

public void setY(double y) {//设置y坐标

this.y = y;

}

public void setZ(double z) {//设置z坐标

this.z = z;

}

public void setPosition(double x,double y,double z){//一次性设置三个坐标的方法

setX(x);

setX(y);

setX(z);

}

// 计算距离 x*x + y*y+z*x,然后开平方

public double getDistance(){

final int square = 2;//平方

return Math.sqrt(Math.pow(x,square) + Math.pow(y,square)+ Math.pow(z,square));

}

public String toString(){//重写输出方法

return "(x,y,z):" + x + "," + y + "," + z;

}

}

-------------

Before new valued set,the point is:(x,y,z):1.0,1.0,1.0

Before new valued set,the distance to (0,0,0) is:1.7320508075688772

After new valued set,the point is:(x,y,z):2.0,1.0,1.0

After new valued set,the distance to (0,0,0) is:2.449489742783178

1年前

1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值