Repository navigation
Expand file tree
/
Copy pathquadtree.min.js
More file actions
8 lines (8 loc) · 1.78 KB
/
Copy pathquadtree.min.js
File metadata and controls
8 lines (8 loc) · 1.78 KB
1
2
3
4
5
6
7
8
/*
* Javascript Quadtree
* @version 1.1.1
* @licence MIT
* @author Timo Hausmann
* https://github.com/timohausmann/quadtree-js/
*/
!function(a,b){function c(a,b,c,d){this.max_objects=b||10,this.max_levels=c||4,this.level=d||0,this.bounds=a,this.objects=[],this.nodes=[]}c.prototype.split=function(){var a=this.level+1,d=b.round(this.bounds.width/2),e=b.round(this.bounds.height/2),f=b.round(this.bounds.x),g=b.round(this.bounds.y);this.nodes[0]=new c({x:f+d,y:g,width:d,height:e},this.max_objects,this.max_levels,a),this.nodes[1]=new c({x:f,y:g,width:d,height:e},this.max_objects,this.max_levels,a),this.nodes[2]=new c({x:f,y:g+e,width:d,height:e},this.max_objects,this.max_levels,a),this.nodes[3]=new c({x:f+d,y:g+e,width:d,height:e},this.max_objects,this.max_levels,a)},c.prototype.getIndex=function(a){var b=-1,c=this.bounds.x+this.bounds.width/2,d=this.bounds.y+this.bounds.height/2,e=a.y<d&&a.y+a.height<d,f=a.y>d;return a.x<c&&a.x+a.width<c?e?b=1:f&&(b=2):a.x>c&&(e?b=0:f&&(b=3)),b},c.prototype.insert=function(a){var b,c=0;if("undefined"!=typeof this.nodes[0]&&(b=this.getIndex(a),-1!==b))return void this.nodes[b].insert(a);if(this.objects.push(a),this.objects.length>this.max_objects&&this.level<this.max_levels)for("undefined"==typeof this.nodes[0]&&this.split();c<this.objects.length;)b=this.getIndex(this.objects[c]),-1!==b?this.nodes[b].insert(this.objects.splice(c,1)[0]):c+=1},c.prototype.retrieve=function(a){var b=this.getIndex(a),c=this.objects;if("undefined"!=typeof this.nodes[0])if(-1!==b)c=c.concat(this.nodes[b].retrieve(a));else for(var d=0;d<this.nodes.length;d+=1)c=c.concat(this.nodes[d].retrieve(a));return c},c.prototype.clear=function(){this.objects=[];for(var a=0;a<this.nodes.length;a+=1)"undefined"!=typeof this.nodes[a]&&this.nodes[a].clear();this.nodes=[]},a.Quadtree=c}(window,Math);