Java calculates the distance between two points. java calculates the distance between two points.
Use the Point class that comes with java
Import java. awt. point; // reference the Point class under the awt package. The function of this class is to represent (x, y) public class Distance {public static void main (String [] args) {Point p1 = new Point (5, 6 ); // define the coordinates of the first vertex (5, 6) Point p2 = new Point (7, 8); // define the coordinates of the second vertex (7, 8) // locate the coordinate System. out. println ("x coordinate of p1 is" + p1.getX (); System. out. println ("the y coordinate of p1 is" + p1.getY (); System. out. println ("p2 x coordinates" + p2.getX (); System. out. println ("y coordinate of p2 is" + p2.getY (); // calculate the distance between two points. double juli = Math. sqrt (Math. abs (p1.getX ()-p2.getX () * (p1.getX ()-p2.getX () + (p1.getY ()-p2.getY () * (p1.getY () -p2.getY (); System. out. println ("the distance between two points is:" + juli );}}
Constructor
Public class Point {double num1, num2; Point (double I, double j) {num1 = I; num2 = j;} void getX () {System. out. println (num1);} void getY () {System. out. println (num2);} public static void main (String [] args) {Point p1 = new Point (5, 6); Point p2 = new Point (7, 8 ); p1.getX (); // xy coordinate p1.getY (); p2.getX (); p2.getY ();/** formula omitted */}}
The above is the method for finding the distance between two points. Thank you for your support for the help house.