当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaFX 类 Point3D用法及代码示例


Point3D类是JavaFX的一部分。此类定义3D空间中的3维点。 Point3D类通过其x,y,z坐标表示3D点。

该类的构造函数是:

  • Point3D(double x, double y, double z):使用指定的坐标创建Point3D对象。

常用方法:


方法 说明
distance(double x, double y, double z) 计算此点与指定点之间的距离
distance(Point3D p) 计算此点与指定Point3D对象之间的距离
equals(java.lang.Object o) 返回该点的哈希码值。
getX() 返回x坐标
getY() 返回y坐标
getZ() 返回z坐标
hashCode() 返回此Point3D对象的哈希码。

下面的程序将说明Point3D类的用法:

  1. Java程序创建一个Point3D对象并显示其坐标并找到其与原点的距离:在此程序中,我们通过传递其x,y,z坐标作为参数来创建一个名为point3d_1的Point3D对象。我们使用getX(),getY(),getZ()函数获取x,y,z参数并显示它。我们还计算点到原点的距离并显示出来。
    // Java program to create a point 3D  
    // object and display its coordinates 
    // and find its distance from origin 
    import javafx.geometry.Point3D; 
      
    public class Point3D_1 { 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // Create a point3D object 
            Point3D point3d_1 = new Point3D(20.0f, 50.0f, 70.0f); 
      
            double x, y, z; 
      
            // get the coordinates of the point 
            x = point3d_1.getX(); 
            y = point3d_1.getY(); 
            z = point3d_1.getZ(); 
      
            // display the coordinates of the point 
            System.out.println("x coordinate = " + x  
                                + ", y coordinate = " 
                                + y + ", z coordinate = " + z); 
      
            // print its distance from origin 
            System.out.println("Distance From Origin = " 
                         + point3d_1.distance(0, 0, 0)); 
        } 
    }

    输出:

    x coordinate = 20.0, y coordinate = 50.0, z coordinate = 70.0
    Distance From Origin = 88.31760866327846
    
  2. Java程序创建3个Point3D对象并显示其坐标和距原点的距离,并检查3个点中的哪个点相似以及两个点之间的距离:在此程序中,我们通过传递其x,y,z坐标作为参数来创建3个Point3D对象,分别命名为point3d_1,point3d_2,point3d_3。我们使用getX(),getY(),getZ()函数获取x,y,z参数并显示它。我们还计算了该点到原点的距离,并针对三个点中的每一个都显示了该距离。我们还使用equals()函数显示两个点是否相等,并使用距离函数显示两个点之间的距离。
    // Java program to create 3 Point3D objects and  
    // display their coordinates and distance from  
    // origin and check which of the 3 points are  
    // similar and their distances between two points 
    import javafx.geometry.Point3D; 
      
    public class Point3D_2 { 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // create three point3D objects 
            Point3D point3d_1 = new Point3D(20.0f, 50.0f, 70.0f); 
            Point3D point3d_2 = new Point3D(20.0f, 50.0f, 70.0f); 
            Point3D point3d_3 = new Point3D(200.0f, 20.0f, 90.0f); 
      
            // display the coordinates of the 3 points 
            display(point3d_1); 
            display(point3d_2); 
            display(point3d_3); 
      
            // check whether any point is equal to other or not 
            System.out.println("Point 1 equals point 2 = " 
                           + point3d_1.equals(point3d_2)); 
      
            System.out.println("Point 2 equals point 3 = " 
                           + point3d_2.equals(point3d_3)); 
      
            System.out.println("Point 3 equals point 1 = " 
                            + point3d_3.equals(point3d_1)); 
      
            // distance between two points 
            System.out.println("Distane between point 1 and point 2 = " 
                                      + point3d_1.distance(point3d_2)); 
      
            System.out.println("Distane between point 2 and point 3 = " 
                                       + point3d_2.distance(point3d_3)); 
      
            System.out.println("Distane between point 3 and point 1 = " 
                                      + point3d_3.distance(point3d_1)); 
        } 
      
        // Display Method 
        public static void display(Point3D point3d) 
        { 
      
            double x, y, z; 
      
            // get the coordinates of the point 
            x = point3d.getX(); 
            y = point3d.getY(); 
            z = point3d.getZ(); 
      
            // display the coordinates of the point 
            System.out.println("x coordinate = " + x  
                               + ", y coordinate = " 
                               + y + ", z coordinate = " + z); 
      
            // print its distance from origin 
            System.out.println("Distance From Origin = " +  
                                point3d.distance(0, 0, 0)); 
        } 
    }

    输出:

    x coordinate = 20.0, y coordinate = 50.0, z coordinate = 70.0
    Distance From Origin = 88.31760866327846
    x coordinate = 20.0, y coordinate = 50.0, z coordinate = 70.0
    Distance From Origin = 88.31760866327846
    x coordinate = 200.0, y coordinate = 20.0, z coordinate = 90.0
    Distance From Origin = 220.22715545545242
    Point 1 equals point 2 = true
    Point 2 equals point 3 = false
    Point 3 equals point 1 = false
    Distane between point 1 and point 2 = 0.0
    Distane between point 2 and point 3 = 183.5755975068582
    Distane between point 3 and point 1 = 183.5755975068582
    

注意:以上程序可能无法在在线IDE中运行。请使用离线编译器。

参考:https://docs.oracle.com/javafx/2/api/javafx/geometry/Point3D.html



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 JavaFX | Point3D Class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。