import {quadtree} from 'd3-quadtree'; import * as Rectangle from './rectangle'; export class QuadTree { constructor() { this.quadTree = quadtree(); } add(datum) { this.quadTree.add(datum); } clear() { const data = this.quadTree.data(); for (let i = 0; i < data.length; i++) { const datum = data[i]; this.quadTree.remove(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; } do { if (Rectangle.intersects(queryRectangle, node.data)) { nodes.push(node); } } while (node = node.next); }); return nodes; } }