定义三维空间的一点及测量其它点到此点的距离

本文介绍了一个简单的三维坐标点类的实现,包括设置坐标值的方法和计算两点间距离的功能。通过示例展示了如何创建点对象并进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. /**
  2. *定义一个三维坐标的点
  3. *能够修改点的三维坐标
  4. *能够测量该点到任意一点的距离
  5. **/
  6. class Point{
  7.     double x, y, z;
  8.     
  9.     Point(double _x, double _y, double _z){ //构造方法定义三维坐标
  10.         x = _x;
  11.         y = _y;
  12.         z = _z;
  13.     }
  14.     
  15.     void setX(double a) { //修改x坐标
  16.         x = a;
  17.     }
  18.     
  19.     void setY(double b) { //修改y坐标
  20.         y = b;
  21.     }
  22.     
  23.     void setZ(double c) { //修改z坐标
  24.         z = c;
  25.     }
  26.     
  27.     double getDistance(Point p) { //取一点返回任意点到此点的距离
  28.         return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z);
  29.     }
  30. }
  31. public class TestPoint {
  32.     public static void main(String[] args) {
  33.         Point p = new Point(1.0,1.0,1.0);
  34.         Point p1 = new Point(0.0,0.0,0.0);
  35.         System.out.println(p.getDistance(p1)); //打印出3.0
  36.         
  37.         p.setX(2.0);
  38.         p.setY(2.0);
  39.         p.setZ(2.0);
  40.         System.out.println(p.x + "-" + p.y + "-" + p.z); //打印出2.0-2.0-2.0
  41.         System.out.println(p.x + p.y + p.z); //打印出6.0
  42.         System.out.println(p.getDistance(new Point(2.0,2.0,3.0))); //打印出1.0
  43.     }
  44. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值