|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
1 | 3 | /** |
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> |
3 | 8 | * |
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> |
6 | 14 | */ |
7 | | -public class DistanceBetweenTwoPoints { |
| 15 | +public final class DistanceBetweenTwoPoints { |
| 16 | + |
| 17 | + private DistanceBetweenTwoPoints() { |
| 18 | + // Utility class; prevent instantiation |
| 19 | + } |
8 | 20 |
|
9 | 21 | /** |
10 | 22 | * Calculate the Euclidean distance between two points. |
11 | 23 | * |
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 |
17 | 29 | */ |
18 | 30 | 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) { |
20 | 35 |
|
21 | 36 | return Math.sqrt( |
22 | 37 | Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) |
23 | 38 | ); |
24 | 39 | } |
25 | | - |
26 | | - public static void main(String[] args) { |
27 | | - System.out.println(calculate(0, 0, 3, 4)); // 5.0 |
28 | | - } |
29 | 40 | } |
0 commit comments