📜  JavaFX | Point3D 类

📅  最后修改于: 2022-05-13 01:55:48.690000             🧑  作者: Mango

JavaFX | Point3D 类

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

类的构造函数是:

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

常用方法:

MethodExplanation
distance(double x, double y, double z)Calculates the distance between this point and the specified point
distance(Point3D p)Calculates the distance between this point and the specified Point3D object
equals(java.lang.Object o)Returns a hash code value for the point.
getX()Returns the x coordinate
getY()Returns the y coordinate
getZ()Returns the z coordinate
hashCode()Returns a hash code for this Point3D object.

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

1. Java程序创建一个 Point3D 对象并显示它的坐标并找到它与原点的距离:在这个程序中,我们通过传递它的 x、y、z 坐标作为参数来创建一个名为 point3d_1 的 Point3D 对象。我们使用getX()getY()getZ()函数获取 x、y、z 参数并显示它。我们还计算点到原点的距离并显示它。

Java
// 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));
    }
}


Java
// 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("Distance between point 1 and point 2 = "
                                  + point3d_1.distance(point3d_2));
 
        System.out.println("Distance between point 2 and point 3 = "
                                   + point3d_2.distance(point3d_3));
 
        System.out.println("Distance 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

2. Java程序创建 3 个 Point3D 对象并显示它们的坐标和到原点的距离,并检查 3 个点中哪一个相似以及它们在两点之间的距离:在这个程序中,我们通过传递创建 3 个 Point3D 对象,命名为 point3d_1、point3d_2、point3d_3它的 x、y、z 坐标作为参数。我们使用getX()getY()getZ()函数获取 x、y、z 参数并显示它。我们还计算点到原点的距离,并为三个点中的每一个显示它。我们还使用equals()函数显示任意两点是否相等,并使用 distance函数显示两点之间的距离。

Java

// 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("Distance between point 1 and point 2 = "
                                  + point3d_1.distance(point3d_2));
 
        System.out.println("Distance between point 2 and point 3 = "
                                   + point3d_2.distance(point3d_3));
 
        System.out.println("Distance 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
Distance between point 1 and point 2 = 0.0
Distance between point 2 and point 3 = 183.5755975068582
Distance between point 3 and point 1 = 183.5755975068582

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

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