當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。