Skip to content

Commit 132394c

Browse files
authored
Add distance between two points algorithm
1 parent fd0bcb7 commit 132394c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Distance Between Two Points in 2D Space
3+
*
4+
* Reference:
5+
* https://en.wikipedia.org/wiki/Euclidean_distance
6+
*/
7+
public class DistanceBetweenTwoPoints {
8+
9+
/**
10+
* Calculate the Euclidean distance between two points.
11+
*
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
17+
*/
18+
public static double calculate(
19+
double x1, double y1, double x2, double y2) {
20+
21+
return Math.sqrt(
22+
Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)
23+
);
24+
}
25+
26+
public static void main(String[] args) {
27+
System.out.println(calculate(0, 0, 3, 4)); // 5.0
28+
}
29+
}

0 commit comments

Comments
 (0)