在三维空间中,求点和点之间的距离并打印。 提示: (1)定义一个类 Point3D 含有成员变量x,y,z (2)类Point3D中有方法 getDistance,可以求得当前的点到另外一个点之前的 距离。 3) Main 方法和运行结果参考如下: public class Experiment_2_1 { public static void main(String[] args) { Point3D p1 = new Point3D( x:1.0, y: 1.0, z:1.0): Point3D p2 = new Point3D( x:2.0, y: 2.0, z: 2.0) ; double distance = p1.getDistance(p2): System.out.println("Ap144p209PE*: " + distance) :
时间: 2023-06-12 17:07:59 浏览: 116
在一个三维空间中 求点到点之间的距离
好的,下面是代码实现:
```
public class Point3D {
private double x;
private double y;
private double z;
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getDistance(Point3D other) {
double dx = this.x - other.x;
double dy = this.y - other.y;
double dz = this.z - other.z;
return Math.sqrt(dx*dx + dy*dy + dz*dz);
}
}
public class Experiment_2_1 {
public static void main(String[] args) {
Point3D p1 = new Point3D(1.0, 1.0, 1.0);
Point3D p2 = new Point3D(2.0, 2.0, 2.0);
double distance = p1.getDistance(p2);
System.out.println("点p1和点p2之间的距离是:" + distance);
}
}
```
运行结果为:
```
点p1和点p2之间的距离是:1.7320508075688772
```
阅读全文