The calculation of distance is an important aspect of mathematics. The space between the two points present in a plane is referred to as the distance. The distance between two points must always be a positive value since the line segment that is responsible for joining the two points can never be negative.
In this write-up, we will describe a method for the calculation of distance between two points in Java.
How to Evaluate the Distance Between Two Points in Java?
In Mathematics, the line segment joining the two points on a plane is referred to as the distance between the two points. The 2D plane consists of four points. For a certain point, let’s suppose P contains two coordinates (x1, y1) and another point Q contains (x2, y2). The formula mentioned below calculates the distance between two points.
Let us save the distance or the output as PQ therefore the formula becomes:
It is commonly referred to as the “Euclidean distance” formula.
Example 1: Calculation of Distance Between Two Points Using Math Library
In the below-mentioned code Math class of Java is imported to use the square root function for the calculation of the distance.
//Import the Math Class
import java.lang.Math.*;
//Declare a Class to calculate the distance
class distance{
public static void main(String arg[])
{
//Enter the integer values
int a1,a2,b1,b2;
double d;
a1=5;
b1=6;
a2=4;
b2=9;
//The Math class has all the integer values for calculation.
d=Math.sqrt((a2-a1)*(a2-a1) + (b2-b1)*(b2-b1));
//The output is printed on the screen. System.out.println("DISTANCE BETWEEN TWO POINTS");
System.out.println("The distance between "+"("+a1+","+b1+") and "+"("+a2+","+b2+") is "+d);
}
}
In the above-mentioned Java code:
- The Math class is imported for certain functions.
- In the Main class, the “a” and “b” coordinates are declared.
- The sqrt() function in the above code helps to determine the distance.
- The println() method prints the distance.
Output
In the output below, the distance between the two points that are (5,6) and (4,9) is printed on the screen.
Example 2: User Input for Calculation of the Distance
The code below implements the scanner package that is “util.Scanner” to obtain the user input and it outputs the distance between the entered inputs.
//Import Scanner package for user input.
import java.util.Scanner;
class distancetwo {
//Scanner class to get the user input
public static void main(String[] args) {
Scanner sa = new Scanner(System.in);
//Declare the integers for the points
int a1,a2,b1,b2,a,b;
//The distance is declared as double
double distance;
//Taking the input as an integer.
System.out.print("Enter a1 point: ");
a1 = sa.nextInt();
System.out.print("Enter b1 point: ");
b1 = sa.nextInt();
//Taking the input as an integer.
System.out.print("Enter a2 point: ");
a2 = sa.nextInt();
System.out.print("Enter b2 point: ");
b2 = sa.nextInt();
//Calculate the distance a = a2 - a1;
b = b2 - b1;
//The Math class for the sqrt function
distance = Math.sqrt(a * a + b * b);
//Print the result for the distance between two points.
System.out.println("Distance between two points = " + distance);
}
}
In the above code:
- The Scanner package is imported to take the user input.
- With the Main class, the integer values for the coordinates are declared.
- The double-type variable stores the distance.
- To get the user input the println() method prompts a message on the screen.
- The nextInt() method in Java fetches the value of the next integer.
- The calculation takes place in such a manner that the subtraction results for x coordinates that are a1 and a2 are saved in a and the same for the y coordinates saved in b.
- The sqrt() function calculates the distance using the distance formula.
- The println() method declares the results.
Output
In the output depicted below, the integer values for the coordinates are entered and the distance is calculated using the sqrt() method of Java.
This sums up the topic discussion on the calculation of distance between two points in Java.
Conclusion
The distance between two points in Java is evaluated using the “Math.sqrt((a2-a1)*(a2-a1) + (b2-b1)*(b2-b1));” equation/formula. The stated formula is commonly referred to as the “Euclidean distance” formula. This write-up has elaborated on the implementation of the Math package to calculate the distance between two points on a plane in Java.