计算三维空间两点距离【马士兵面向对象课后习题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();
}
}
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();
}
}
相关文章推荐
- 《课后习题---求两点之间的距离》
- sql server2008根据经纬度计算两点之间的距离
- 计算地球上A B两点间距离的计算公式
- 2001 计算两点间的距离
- PHP实现搜索地理位置及计算两点地理位置间距离的实例
- 【百度地图API】如何根据摩卡托坐标进行POI查询,和计算两点距离
- C#根据两点经纬度计算距离
- 经纬度知识-来源,计算,根据两点经纬度计算距离
- HDU2001 计算两点间的距离【入门】
- 计算两点间距离(成员函数)
- 计算地球上两点的直线距离
- HDOJ 【C】2001 计算两点间的距离
- java-经纬度有关的计算(半径内的经纬度范围和两点间距离)
- Windows 8 地理位置定位 4.根据经纬度计算地面两点间的距离
- C#计算两点间的距离
- iOS定位操作,获取当前位置,计算两点之间距离
- PHP根据两点间的坐标计算距离
- 根据两点经纬度计算距离 whh博客转载
- 利用JS实现的根据经纬度计算地球上两点之间的距离
- HDU2001_计算两点间的距离