首先定义Point类
public class Point {
private int x;
private int y;
private int z;
//无参构造
public Point(){}
//带参数的构造函数,用于初始化坐标
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
}
编写Main方法,执行,验证
import java.lang.Math;
public class Main {
public static double distancePOW(Point p1,Point p2)
{
return Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2)+Math.pow(p1.getZ()-p2.getZ(),2);
}
public static void main(String[] args){
Point point1 = new Point(1,2,3);
Point point2 = new Point(5,0,0);
double distancePOW = distancePOW(point1,point2);
System.out.println("两个三维坐标点之间的距离的平方为"+distancePOW);
}
}
输出结果:两个三维坐标点之间的距离的平方为29.0
温馨提示:答案为网友推荐,仅供参考