Open In App

JavaFX | Point3D Class

Last Updated : 13 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Point3D class is a part of JavaFX. This class defines a 3-dimensional point in a 3D space. The Point3D class represents a 3D point by its x, y, z coordinates.

Constructor of the class is: 

  • Point3D(double x, double y, double z): Creates a Point3D object using the specified coordinates.

Commonly Used Methods: 

Method Explanation
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.

Below programs will illustrate the use of the Point3D class:  

1. Java program to create a Point3D object and display its coordinates and find its distance from the origin: In this program we create a Point3D object named point3d_1 by passing its x, y, z coordinates as arguments. We get the x, y, z argument using the getX(), getY(), getZ() function and display it. We also calculate the distance of the point from the origin and also display it.

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


Output: 

x coordinate = 20.0, y coordinate = 50.0, z coordinate = 70.0
Distance From Origin = 88.31760866327846

2. Java program to create 3 Point3D objects and display their coordinates and distance from the origin and check which of the 3 points are similar and their distances between two points: In this program we create 3 Point3D objects named point3d_1, point3d_2, point3d_3 by passing its x, y, z coordinates as arguments. we get the x, y, z argument using the getX(), getY(), getZ() function and display it. We also calculate the distance of the point from the origin and also display it for each of the three points. We also display whether any two points are equal or not using equals() function and also display the distance between two points using the distance function.

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


Output: 

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

Note: The above programs might not run in an online IDE. Please use an offline compiler.

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads