Title: [BUG] Incorrect coordinate subtraction in DistanceFormula.euclideanDistance · Issue #7746 · TheAlgorithms/Java · GitHub
Open Graph Title: [BUG] Incorrect coordinate subtraction in DistanceFormula.euclideanDistance · Issue #7746 · TheAlgorithms/Java
X Title: [BUG] Incorrect coordinate subtraction in DistanceFormula.euclideanDistance · Issue #7746 · TheAlgorithms/Java
Description: Description 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: public static double euclideanD...
Open Graph Description: Description 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-axi...
X Description: Description 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-axi...
Opengraph URL: https://github.com/TheAlgorithms/Java/issues/7746
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG] Incorrect coordinate subtraction in DistanceFormula.euclideanDistance","articleBody":"### Description\nIn `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:\n\n```java\npublic static double euclideanDistance(double x1, double y1, double x2, double y2) {\n double dX = Math.pow(x2 - x1, 2);\n double dY = Math.pow(y2 - x1, 2); // \u003c--- Bug: x1 used instead of y1\n return Math.sqrt(dX + dY);\n}\n```\n\nAs 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}$.\n\nIn 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)`).\n\n### Steps to reproduce\n```java\n// Calculating Euclidean distance between (1, 3) and (8, 0)\n// dx = 8 - 1 = 7, dy = 0 - 3 = -3\n// Expected: sqrt(7^2 + (-3)^2) = sqrt(58) ≈ 7.615773105863909\ndouble distance = DistanceFormula.euclideanDistance(1, 3, 8, 0);\nSystem.out.println(distance); // Prints 7.0710678118654755 (which is sqrt(50))\n```\n\n### Expected behavior\n`DistanceFormula.euclideanDistance(x1, y1, x2, y2)` should subtract $y_1$ from $y_2$ (`Math.pow(y2 - y1, 2)`), correctly yielding $\\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$.","author":{"url":"https://github.com/spidervirus","@type":"Person","name":"spidervirus"},"datePublished":"2026-11-11T14:36:13Z","url":"https://github.com/TheAlgorithms/Java/issues/7746"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c2616503-6020-04f4-b342-860f54da90d7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D770:3166AC:4B6EE76:6413441:6AFA8A1B |
| html-safe-nonce | 2444624a4f677c8675804edbd606f593ac31575e60b28451ce7195def9669b1d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzcwOjMxNjZBQzo0QjZFRTc2OjY0MTM0NDE6NkFGQThBMUIiLCJ2aXNpdG9yX2lkIjoiODkyOTg3NzM3MjYyMjc2ODY2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d74b36a1b0a4ce0689ae0082bbf3a202b1be4807ea197bd8b7c0be786d5d0ee9 |
| hovercard-subject-tag | issue:6330710765 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/Java/7746/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3ee5faba1b9bcc51aa463276e8877f2716e79041ce7bf94c4103f0dd9e1f84a1/TheAlgorithms/Java/issues/7746 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3ee5faba1b9bcc51aa463276e8877f2716e79041ce7bf94c4103f0dd9e1f84a1/TheAlgorithms/Java/issues/7746 |
| og:image:alt | Description 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-axi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | spidervirus |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/Java git https://github.com/TheAlgorithms/Java.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 63477660 |
| octolytics-dimension-repository_nwo | TheAlgorithms/Java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 63477660 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/Java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width