2024-02-14 13:02:11 -05:00
|
|
|
function tree_add(t){const e=+this._x.call(null,t),i=+this._y.call(null,t);return add(this.cover(e,i),e,i,t)}function add(t,e,i,r){if(isNaN(e)||isNaN(i))return t;var n,h,s,a,o,u,l,_,d,f=t._root,y={data:r},x=t._x0,c=t._y0,v=t._x1,w=t._y1;if(!f)return t._root=y,t;while(f.length){(u=e>=(h=(x+v)/2))?x=h:v=h;(l=i>=(s=(c+w)/2))?c=s:w=s;if(n=f,!(f=f[_=l<<1|u]))return n[_]=y,t}a=+t._x.call(null,f.data);o=+t._y.call(null,f.data);if(e===a&&i===o)return y.next=f,n?n[_]=y:t._root=y,t;do{n=n?n[_]=new Array(4):t._root=new Array(4);(u=e>=(h=(x+v)/2))?x=h:v=h;(l=i>=(s=(c+w)/2))?c=s:w=s}while((_=l<<1|u)===(d=(o>=s)<<1|a>=h));return n[d]=f,n[_]=y,t}function addAll(t){var e,i,r,n,h=t.length,s=new Array(h),a=new Array(h),o=Infinity,u=Infinity,l=-Infinity,_=-Infinity;for(i=0;i<h;++i)if(!isNaN(r=+this._x.call(null,e=t[i]))&&!isNaN(n=+this._y.call(null,e))){s[i]=r;a[i]=n;r<o&&(o=r);r>l&&(l=r);n<u&&(u=n);n>_&&(_=n)}if(o>l||u>_)return this;this.cover(o,u).cover(l,_);for(i=0;i<h;++i)add(this,s[i],a[i],t[i]);return this}function tree_cover(t,e){if(isNaN(t=+t)||isNaN(e=+e))return this;var i=this._x0,r=this._y0,n=this._x1,h=this._y1;if(isNaN(i)){n=(i=Math.floor(t))+1;h=(r=Math.floor(e))+1}else{var s,a,o=n-i||1,u=this._root;while(i>t||t>=n||r>e||e>=h){a=(e<r)<<1|t<i;s=new Array(4),s[a]=u,u=s,o*=2;switch(a){case 0:n=i+o,h=r+o;break;case 1:i=n-o,h=r+o;break;case 2:n=i+o,r=h-o;break;case 3:i=n-o,r=h-o;break}}this._root&&this._root.length&&(this._root=u)}this._x0=i;this._y0=r;this._x1=n;this._y1=h;return this}function tree_data(){var t=[];this.visit((function(e){if(!e.length)do{t.push(e.data)}while(e=e.next)}));return t}function tree_extent(t){return arguments.length?this.cover(+t[0][0],+t[0][1]).cover(+t[1][0],+t[1][1]):isNaN(this._x0)?void 0:[[this._x0,this._y0],[this._x1,this._y1]]}function Quad(t,e,i,r,n){this.node=t;this.x0=e;this.y0=i;this.x1=r;this.y1=n}function tree_find(t,e,i){var r,n,h,s,a,o,u,l=this._x0,_=this._y0,d=this._x1,f=this._y1,y=[],x=this._root;x&&y.push(new Quad(x,l,_,d,f));if(null==i)i=Infinity;else{l=t-i,_=e-i;d=t+i,f=e+i;i*=i}while(o=y.pop())if(!(!(x=o.node)||(n=o.x0)>d||(h=o.y0)>f||(s=o.x1)<l||(a=o.y1)<_))if(x.length){var c=(n+s)/2,v=(h+a)/2;y.push(new Quad(x[3],c,v,s,a),new Quad(x[2],n,v,c,a),new Quad(x[1],c,h,s,v),new Quad(x[0],n,h,c,v));if(u=(e>=v)<<1|t>=c){o=y[y.length-1];y[y.length-1]=y[y.length-1-u];y[y.length-1-u]=o}}else{var w=t-+this._x.call(null,x.data),p=e-+this._y.call(null,x.data),N=w*w+p*p;if(N<i){var g=Math.sqrt(i=N);l=t-g,_=e-g;d=t+g,f=e+g;r=x.data}}return r}function tree_remove(t){if(isNaN(h=+this._x.call(null,t))||isNaN(s=+this._y.call(null,t)))return this;var e,i,r,n,h,s,a,o,u,l,_,d,f=this._root,y=this._x0,x=this._y0,c=this._x1,v=this._y1;if(!f)return this;if(f.length)while(true){(u=h>=(a=(y+c)/2))?y=a:c=a;(l=s>=(o=(x+v)/2))?x=o:v=o;if(!(e=f,f=f[_=l<<1|u]))return this;if(!f.length)break;(e[_+1&3]||e[_+2&3]||e[_+3&3])&&(i=e,d=_)}while(f.data!==t)if(!(r=f,f=f.next))return this;(n=f.next)&&delete f.next;if(r)return n?r.next=n:delete r.next,this;if(!e)return this._root=n,this;n?e[_]=n:delete e[_];(f=e[0]||e[1]||e[2]||e[3])&&f===(e[3]||e[2]||e[1]||e[0])&&!f.length&&(i?i[d]=f:this._root=f);return this}function removeAll(t){for(var e=0,i=t.length;e<i;++e)this.remove(t[e]);return this}function tree_root(){return this._root}function tree_size(){var t=0;this.visit((function(e){if(!e.length)do{++t}while(e=e.next)}));return t}function tree_visit(t){var e,i,r,n,h,s,a=[],o=this._root;o&&a.push(new Quad(o,this._x0,this._y0,this._x1,this._y1));while(e=a.pop())if(!t(o=e.node,r=e.x0,n=e.y0,h=e.x1,s=e.y1)&&o.length){var u=(r+h)/2,l=(n+s)/2;(i=o[3])&&a.push(new Quad(i,u,l,h,s));(i=o[2])&&a.push(new Quad(i,r,l,u,s));(i=o[1])&&a.push(new Quad(i,u,n,h,l));(i=o[0])&&a.push(new Quad(i,r,n,u,l))}return this}function tree_visitAfter(t){var e,i=[],r=[];this._root&&i.push(new Quad(this._root,this._x0,this._y0,this._x1,this._y1));while(e=i.pop()){var n=e.node;if(n.length){var h,s=e.x0,a=e.y0,o=e.x1,u=e.y1,l=(s+o)/2,_=(a+u)/2;(h=n[0])&&i.push(new Quad(h,s,a,l,_));(h=n[1])&&i.push(new Quad(h,l,a,o,_));(h=n[2])&&i.push(new Quad(h,s,_,l,u));(h
|