-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
49 lines (40 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Queue from 'tinyqueue';
export default function knn(tree, x, y, n, predicate, maxDistance) {
let node = tree.data;
const result = [];
const toBBox = tree.toBBox;
const queue = new Queue(undefined, compareDist);
while (node) {
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
const dist = boxDist(x, y, node.leaf ? toBBox(child) : child);
if (!maxDistance || dist <= maxDistance * maxDistance) {
queue.push({
node: child,
isItem: node.leaf,
dist
});
}
}
while (queue.length && queue.peek().isItem) {
const candidate = queue.pop().node;
if (!predicate || predicate(candidate))
result.push(candidate);
if (n && result.length === n) return result;
}
node = queue.pop();
if (node) node = node.node;
}
return result;
}
function compareDist(a, b) {
return a.dist - b.dist;
}
function boxDist(x, y, box) {
const dx = axisDist(x, box.minX, box.maxX),
dy = axisDist(y, box.minY, box.maxY);
return dx * dx + dy * dy;
}
function axisDist(k, min, max) {
return k < min ? min - k : k <= max ? 0 : k - max;
}