Skip to content

Distance calculation subtracts instead of adding #3

@ecashin

Description

@ecashin

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$

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions