Skip to content

Commit 25b090d

Browse files
authored
DistanceBetweenTwoPoints.java
1 parent 99ea204 commit 25b090d

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
1+
package com.thealgorithms.maths;
2+
13
/**
2-
* Distance Between Two Points in 2D Space
4+
* Distance Between Two Points in 2D Space.
5+
*
6+
* <p>This class provides a method to calculate the Euclidean distance
7+
* between two points in a two-dimensional plane.</p>
38
*
4-
* Reference:
5-
* https://en.wikipedia.org/wiki/Euclidean_distance
9+
* <p>Formula:
10+
* d = √((x2 − x1)² + (y2 − y1)²)</p>
11+
*
12+
* <p>Reference:
13+
* https://en.wikipedia.org/wiki/Euclidean_distance</p>
614
*/
7-
public class DistanceBetweenTwoPoints {
15+
public final class DistanceBetweenTwoPoints {
16+
17+
private DistanceBetweenTwoPoints() {
18+
// Utility class; prevent instantiation
19+
}
820

921
/**
1022
* Calculate the Euclidean distance between two points.
1123
*
12-
* @param x1 x-coordinate of first point
13-
* @param y1 y-coordinate of first point
14-
* @param x2 x-coordinate of second point
15-
* @param y2 y-coordinate of second point
16-
* @return Euclidean distance
24+
* @param x1 x-coordinate of the first point
25+
* @param y1 y-coordinate of the first point
26+
* @param x2 x-coordinate of the second point
27+
* @param y2 y-coordinate of the second point
28+
* @return Euclidean distance between the two points
1729
*/
1830
public static double calculate(
19-
double x1, double y1, double x2, double y2) {
31+
final double x1,
32+
final double y1,
33+
final double x2,
34+
final double y2) {
2035

2136
return Math.sqrt(
2237
Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)
2338
);
2439
}
25-
26-
public static void main(String[] args) {
27-
System.out.println(calculate(0, 0, 3, 4)); // 5.0
28-
}
2940
}

0 commit comments

Comments
 (0)