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

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

Open
TheColorman opened this issue Dec 23, 2022 · 0 comments
Open

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

TheColorman opened this issue Dec 23, 2022 · 0 comments

Comments

@TheColorman
Copy link

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.

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