黑马程序员技术交流社区

标题: 三维空间的一点到圆点的距离 [打印本页]

作者: 城市儒侠    时间: 2013-12-15 21:53
标题: 三维空间的一点到圆点的距离
本帖最后由 城市儒侠 于 2013-12-16 08:23 编辑

基本思路我知道。但是具体怎么算那个距离啊?

  1. class Point {
  2.         double x, y, z;
  3.         
  4.         Point(double _x, double _y, double _z) {
  5.                 x = _x;
  6.                 y = _y;
  7.                 z = _z;
  8.         }
  9.         
  10. double getDistance(Point p) {
  11.    
  12.         }
  13.         
  14. }

  15. public class TestPoint {
  16.         public static void main(String[] args) {
  17.                 Point p = new Point(154.0, 342.0, 54.0);
  18.                 Point p1 = new Point(0.0, 0.0, 0.0);
  19.                 System.out.println(p.getDistance(p1));
  20.                
  21. }
  22. }
复制代码




作者: 回音    时间: 2013-12-15 22:29
class Point
{
        double x, y, z;

        Point(double _x, double _y, double _z)
        {
                x = _x;
                y = _y;
                z = _z;
        }

        double getDistance(Point p)
        {
               return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2) + Math.pow(this.z - p.z, 2));
        }

}

public class Test
{
        public static void main(String[] args)
        {
                Point p = new Point(1, 1, 1);
                Point p1 = new Point(0.0, 0.0, 0.0);
                System.out.println(p.getDistance(p1));
        }
}
距离是用三维空间中的勾股定理计算的。






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2