Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance calculation subtracts instead of adding #3

Open
ecashin opened this issue Jun 27, 2023 · 0 comments
Open

Distance calculation subtracts instead of adding #3

ecashin opened this issue Jun 27, 2023 · 0 comments

Comments

@ecashin
Copy link

ecashin commented Jun 27, 2023

Hi! In the addRandomNode routine of today's mainline branch, line 50 of manyBodySampled.js, a value called a distance is calculated not by summing the squared dimension-wise differences, but by subtracting. The resulting value is compared against a distance calculated conventionally on line 29, by summing the squared differences.

This appears to be a bug that would cause the randomly selected new neighbor to replace an element of the current list more often than the algorithm and the code comments suggest it should. In that case, the fix is the one-character change shown below.

Should I make a pull request?

bash$ git diff
diff --git a/src/manyBodySampled.js b/src/manyBodySampled.js
index 9a6220b..65cf91b 100644
--- a/src/manyBodySampled.js
+++ b/src/manyBodySampled.js
@@ -47,7 +47,7 @@ export default function() {
     while (++i < node.nearest.length) {
       currIdx = node.nearest[i];
       currNode = nodes[currIdx];
-      currDist = (node.x - currNode.x) * (node.x - currNode.x) - (node.y - currNode.y) * (node.y - currNode.y);
+      currDist = (node.x - currNode.x) * (node.x - currNode.x) + (node.y - currNode.y) * (node.y - currNode.y);
       if (currDist > maxDist) {
         maxI = i;
         maxDist = currDist;
bash$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant