Java calculates the distance between two points. java calculates the distance between two points.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.