Okay, here's a Java function to calculate the Euclidean distance between two points in a 2D Cartesian coordinate system.
This uses the standard distance formula derived from the Pythagorean theorem:
distance=(x2−x1)2+(y2−y1)2
import java.lang.Math; // Import the Math class for sqrt and pow
public class CoordinateCalculator {
/**
* Calculates the Euclidean distance between two points (x1, y1) and (x2, y2).
*
* @param x1 The x-coordinate of the first point.
* @param y1 The y-coordinate of the first point.
* @param x2 The x-coordinate of the second point.
* @param y2 The y-coordinate of the second point.
* @return The distance between the two points as a double.
*/
public static double calculateDistance(double x1, double y1, double x2, double y2) {
// Calculate the difference in the x-coordinates
double deltaX = x2 - x1;
// Calculate the difference in the y-coordinates
double deltaY = y2 - y1;
// Square the differences and sum them
// Using multiplication (deltaX * deltaX) is often slightly more efficient than Math.pow(deltaX, 2)
double distanceSquared = (deltaX * deltaX) + (deltaY * deltaY);
// Calculate the square root of the sum of squares
double distance = Math.sqrt(distanceSquared);
return distance;
}
// --- Example Usage ---
public static void main(String[] args) {
// Define two points
double point1X = 1.0;
double point1Y = 2.0;
double point2X = 4.0;
double point2Y = 6.0;
// Calculate the distance using the function
double distance = calculateDistance(point1X, point1Y, point2X, point2Y);
// Print the result
System.out.println("Coordinates of Point 1: (" + point1X + ", " + point1Y + ")");
System.out.println("Coordinates of Point 2: (" + point2X + ", " + point2Y + ")");
System.out.println("The distance between the two points is: " + distance); // Output should be 5.0
// Another example
double xA = -2.5;
double yA = 3.0;
double xB = 1.0;
double yB = -1.0;
double distance2 = calculateDistance(xA, yA, xB, yB);
System.out.println("\nCoordinates of Point A: (" + xA + ", " + yA + ")");
System.out.println("Coordinates of Point B: (" + xB + ", " + yB + ")");
System.out.println("The distance between the two points is: " + distance2); // Output should be 5.0
}
}
Explanation:
import java.lang.Math;: This line imports the Math class, which provides useful mathematical functions like sqrt (square root) and pow (power).
public static double calculateDistance(...):
public: The method can be accessed from any other class.
static: The method belongs to the CoordinateCalculator class itself, not to any specific object instance. You can call it directly using the class name: CoordinateCalculator.calculateDistance(...).
double: The method returns a double value, representing the calculated distance.
(...): The method takes four double arguments: the x and y coordinates of the first point (x1, y1) and the x and y coordinates of the second point (x2, y2).
double deltaX = x2 - x1;: Calculates the difference between the x-coordinates.
double deltaY = y2 - y1;: Calculates the difference between the y-coordinates.
double distanceSquared = (deltaX * deltaX) + (deltaY * deltaY);: Squares both differences and adds them together. This corresponds to (x.
double distance = Math.sqrt(distanceSquared);: Calculates the square root of the sum obtained in the previous step using Math.sqrt(). This gives the final distance.
return distance;: Returns the calculated distance.
main Method: The main method provides a simple example of how to call the calculateDistance function and print the result.