avocado-old/packages/math/quadtree.js
2019-04-16 23:59:08 -05:00

39 lines
707 B
JavaScript

import {quadtree} from 'd3-quadtree';
import * as Rectangle from './rectangle';
export class QuadTree {
constructor() {
this.quadTree = quadtree();
}
add(datum) {
this.quadTree.add(datum);
}
remove(datum) {
this.quadTree.remove(datum);
}
search(queryRectangle) {
const nodes = [];
this.quadTree.visit((node, x, y, x0, y0) => {
const w = x0 - x;
const h = y0 - y;
if (!Rectangle.intersects(queryRectangle, [x, y, w, h])) {
return true;
}
if (4 === node.length) {
return;
}
if (!Rectangle.intersects(queryRectangle, node.data)) {
return;
}
nodes.push(node);
});
return nodes;
}
}