您的位置:首页 > 其它

计算三维空间两点距离【马士兵面向对象课后习题1】

2016-11-06 19:09 288 查看
class Point{
double x;double y;double z;double s;
Point (double _x,double _y,double _z)
{
x=_x;y=_y;z=_z;
}
void setx(double _x){
x=_x;
}
void sety(double _y){
y=_y;
}
void setz(double _z){
z=_z;
}
void sets(){
s=Math.sqrt( Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2) );
}
void display(){
System.out.println("x="+x+"  "+"y="+y+"  "+"z="+z);
System.out.println("s="+s);
}

double getDistance(Point p)
{
return (x - p.x)*(x - p.x)+(y - p.y)*(y - p.y)+(z - p.z)*(z - p.z);
}

}

public class Test{
public static void main(String [] args)
{
Test test=new Test();
Point p1=new Point(2.0,2.0,2.0);

test.gets(p1);
p1.display();//自己写的,麻烦了

Point p2=new Point(1.0,1.0,1.0);
p2.sets();
p2.display();

Point p3=new Point(4.0,4.0,4.0);
p3.sets();
p3.display();

Point p4=new Point(5.0,5.0,5.0);
System.out.print("p1到p4之间距离的平方="+p1.getDistance(p4));//p1到p4的距离的平方,扩展方法,老师的
}

void gets(Point p){
p.sets();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: