You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Incorrect coordinate subtraction in DistanceFormula.euclideanDistance #7746
In com.thealgorithms.maths.DistanceFormula.java, the method euclideanDistance(double x1, double y1, double x2, double y2) incorrectly subtracts x1 instead of y1 when computing the y-axis delta:
As a result, it computes $\sqrt{(x_2 - x_1)^2 + (y_2 - x_1)^2}$ instead of the Euclidean distance $\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$.
In addition, DistanceFormulaTest.java hardcoded incorrect expected values that were produced by the buggy formula (for example, expecting $\sqrt{50} \approx 7.0710678$ instead of the true Euclidean distance $\sqrt{58} \approx 7.6157731$ for points (1, 3) and (8, 0)).
Steps to reproduce
// Calculating Euclidean distance between (1, 3) and (8, 0)// dx = 8 - 1 = 7, dy = 0 - 3 = -3// Expected: sqrt(7^2 + (-3)^2) = sqrt(58) ≈ 7.615773105863909doubledistance = DistanceFormula.euclideanDistance(1, 3, 8, 0);
System.out.println(distance); // Prints 7.0710678118654755 (which is sqrt(50))
Activity