Java 三维空间点类及距离计算:示例和代码
下面是用 Java 语言编写的 Point 类和 Point 的测试类:
Point 类:
public class Point {
private double x;
private double y;
private double z;
public Point() {
this.x = 0;
this.y = 0;
this.z = 0;
}
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setZ(double z) {
this.z = z;
}
public double distanceToOrigin() {
return Math.sqrt(x*x + y*y + z*z);
}
}
PointTest 测试类:
import java.util.Scanner;
public class PointTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print('请输入点的x坐标: ');
double x = scanner.nextDouble();
System.out.print('请输入点的y坐标: ');
double y = scanner.nextDouble();
System.out.print('请输入点的z坐标: ');
double z = scanner.nextDouble();
Point point = new Point(x, y, z);
double distance = point.distanceToOrigin();
System.out.println('点到原点的距离为: ' + distance);
scanner.close();
}
}
你可以在控制台上输入点的坐标,然后程序会计算该点到原点的距离并输出。
原文地址: https://www.cveoy.top/t/topic/w6S 著作权归作者所有。请勿转载和采集!