UNPKG

1.75 kBJavaScriptView Raw
1/* https://github.com/timohausmann/quadtree-js.git v1.2.6 */
2!function(){function o(t,e,s,h){this.max_objects=e||10,this.max_levels=s||4,this.level=h||0,this.bounds=t,this.objects=[],this.nodes=[]}o.prototype.split=function(){var t=this.level+1,e=this.bounds.width/2,s=this.bounds.height/2,h=this.bounds.x,i=this.bounds.y;this.nodes[0]=new o({x:h+e,y:i,width:e,height:s},this.max_objects,this.max_levels,t),this.nodes[1]=new o({x:h,y:i,width:e,height:s},this.max_objects,this.max_levels,t),this.nodes[2]=new o({x:h,y:i+s,width:e,height:s},this.max_objects,this.max_levels,t),this.nodes[3]=new o({x:h+e,y:i+s,width:e,height:s},this.max_objects,this.max_levels,t)},o.prototype.getIndex=function(t){var e=[],s=this.bounds.x+this.bounds.width/2,h=this.bounds.y+this.bounds.height/2,i=t.y<h,o=t.x<s,n=t.x+t.width>s,d=t.y+t.height>h;return i&&n&&e.push(0),o&&i&&e.push(1),o&&d&&e.push(2),n&&d&&e.push(3),e},o.prototype.insert=function(t){var e,s=0;if(this.nodes.length)for(e=this.getIndex(t),s=0;s<e.length;s++)this.nodes[e[s]].insert(t);else if(this.objects.push(t),this.objects.length>this.max_objects&&this.level<this.max_levels){for(this.nodes.length||this.split(),s=0;s<this.objects.length;s++){e=this.getIndex(this.objects[s]);for(var h=0;h<e.length;h++)this.nodes[e[h]].insert(this.objects[s])}this.objects=[]}},o.prototype.retrieve=function(t){var e=this.getIndex(t),s=this.objects;if(this.nodes.length)for(var h=0;h<e.length;h++)s=s.concat(this.nodes[e[h]].retrieve(t));return 0===this.level?Array.from(new Set(s)):s},o.prototype.clear=function(){this.objects=[];for(var t=0;t<this.nodes.length;t++)this.nodes.length&&this.nodes[t].clear();this.nodes=[]},"undefined"!=typeof module&&void 0!==module.exports?module.exports=o:window.Quadtree=o}();
\No newline at end of file