flow: perf

This commit is contained in:
cha0s 2019-05-02 08:39:52 -05:00
parent b8fb92f961
commit 7ccb89226c

View File

@ -5,7 +5,10 @@ import * as Rectangle from './rectangle';
export class QuadTree { export class QuadTree {
constructor() { constructor() {
this._visitNodes = [];
this._queryRectangle = [0, 0, 0, 0];
this.quadTree = quadtree(); this.quadTree = quadtree();
this.onVisit = this.onVisit.bind(this);
} }
add(datum) { add(datum) {
@ -20,28 +23,31 @@ export class QuadTree {
} }
} }
onVisit(node, x, y, x0, y0) {
const w = x0 - x;
const h = y0 - y;
if (!Rectangle.intersects(this._queryRectangle, [x, y, w, h])) {
return true;
}
if (4 === node.length) {
return;
}
do {
if (Rectangle.isTouching(this._queryRectangle, node.data)) {
this.nodes.push(node);
}
} while (node = node.next);
}
remove(datum) { remove(datum) {
this.quadTree.remove(datum); this.quadTree.remove(datum);
} }
search(queryRectangle) { search(queryRectangle) {
const nodes = []; this._queryRectangle = queryRectangle;
this.quadTree.visit((node, x, y, x0, y0) => { this.nodes = [];
const w = x0 - x; this.quadTree.visit(this.onVisit);
const h = y0 - y; return this.nodes;
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;
} }
} }