-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquadtree.min.js
1 lines (1 loc) · 2.92 KB
/
quadtree.min.js
1
"use strict";var Quadtree=function(t,i){this.box=t,this.children=null,this.value=[],this.max=i||10};Quadtree.prototype.insert=function(t,i){if(!this.box.contains(t))return this;var e;if(null===this.children&&this.value.length<this.max){for(e=0;e<this.value.length;e++)if(this.value[e].point.equals(t))return void(this.value[e].value=i);return this.value.push({point:t,value:i}),this}for(null===this.children&&this.subdivide(),e=0;e<this.children.length;e++)this.children[e].insert(t,i);return this.value=[],this},Quadtree.prototype.subdivide=function(){this.children=this.box.split();for(var t=0;t<this.children.length;t++)this.children[t]=new Quadtree(this.children[t],this.max);for(t=0;t<this.value.length;t++)for(var i=0;i<this.children.length;i++)this.children[i].insert(this.value[t].point,this.value[t].value)},Quadtree.prototype.queryRange=function(t){var i=[];return this._queryRangeRec(t,i),i},Quadtree.prototype._queryRangeRec=function(t,i){if(this.box.overlaps(t)){var e;if(this.value.length>0)for(e=0;e<this.value.length;e++)t.contains(this.value[e].point)&&i.push(this.value[e]);else if(null===this.children);else for(e=0;e<this.children.length;e++)this.children[e]._queryRangeRec(t,i)}},Quadtree.prototype.queryPoint=function(t){if(!this.box.contains(t))return null;if(this.value.length>0)for(var i=0;i<this.value.length;i++)if(this.value[i].point.equals(t))return this.value[i].value;if(null!==this.children){for(var e=null,i=0;i<this.children.length;i++)e=e||this.children[i].queryPoint(t);return e}return null},Quadtree.prototype.removePoint=function(t){if(this.box.contains(t)){var i;if(this.value.length>0){for(i=0;i<this.value.length;i++)if(this.value[i].point.equals(t))return void this.value.splice(i,1)}else if(null!==this.children)for(i=0;i<this.children.length;i++)this.children[i].removePoint(t)}},Quadtree.prototype.clear=function(){this.children=null,this.value=[]};var Box=function(t,i){this.low=t,this.high=i};Box.prototype.contains=function(t){return this.low.lte(t)&&this.high.gte(t)?!0:!1},Box.prototype.overlaps=function(t){return this.contains(t.low)||this.contains(t.high)||t.contains(this.low)||t.contains(this.high)?!0:!1},Box.prototype.split=function(){var t=[];return t.push(new Box(this.low,new Point((this.low.x+this.high.x)/2,(this.low.y+this.high.y)/2))),t.push(new Box(new Point((this.low.x+this.high.x)/2,this.low.y),new Point(this.high.x,(this.low.y+this.high.y)/2))),t.push(new Box(new Point((this.low.x+this.high.x)/2,(this.low.y+this.high.y)/2),this.high)),t.push(new Box(new Point(this.low.x,(this.low.y+this.high.y)/2),new Point((this.low.x+this.high.x)/2,this.high.y))),t};var Point=function(t,i){this.x=t,this.y=i};Point.prototype.lte=function(t){return this.x<=t.x&&this.y<=t.y?!0:!1},Point.prototype.gte=function(t){return this.x>=t.x&&this.y>=t.y?!0:!1},Point.prototype.equals=function(t){return this.x===t.x&&this.y===t.y},"undefined"!=typeof module&&(module.exports.Quadtree=Quadtree,module.exports.Box=Box,module.exports.Point=Point);