Skip to content

Not choosing random node if multiple nodes have same distance #32

Description

@TheColorman

I originally posted this issue to knn, but I realised that the issue was due to a limitation here (#19).

If there are 2 nearest nodes with the exact same distance to the test node, the returned node will always be the last one.
Example:

var points = [
    { x: 1, y: 2, label: 'A' },
    { x: 3, y: 4, label: 'B' },
    { x: 5, y: 6, label: 'C' },
    { x: 5, y: 6, label: 'E' },
    { x: 7, y: 8, label: 'D' },
];

var distance = function (a, b) {
    return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
}

var tree = new kdTree(points, distance, ["x", "y"]);

var nearest = tree.nearest({ x: 5, y: 5 }, 1);

console.log(nearest);
// > [ [ { x: 5, y: 6, label: 'E' }, 1 ] ]

Both node E and node C have the same distance from the test node (distance=1), yet node E is always returned no matter what.

Expected behaviour:
If multiple nodes could be chosen as the nearest node and maxNodes is set to 1, the returned node should be chosen at random.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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