-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadtree.min.js
1 lines (1 loc) · 1.8 KB
/
quadtree.min.js
1
function quadtree(e,t,n,r,i){function s(e,t,n,r,i,o){t.x=n;t.y=r;t.x1=i;t.y1=o;if(e.length==1){t.points=e;return}var u=true;for(var a=1;a<e.length;a++){if(String(e[a])!==String(e[0])){u=false;break}}if(u){t.points=e;return}t[0]={};t[1]={};t[2]={};t[3]={};var f=[];var l=[];var c=[];var h=[];for(var a=0;a<e.length;a++){var p=e[a];if(p[0]<(n+i)/2){if(p[1]<(r+o)/2){f.push(p)}else{c.push(p)}}else{if(p[1]<(r+o)/2){l.push(p)}else{h.push(p)}}}if(f.length>0){s(f,t[0],n,r,(n+i)/2,(r+o)/2)}if(l.length>0){s(l,t[1],(n+i)/2,r,i,(r+o)/2)}if(c.length>0){s(c,t[2],n,(r+o)/2,(n+i)/2,o)}if(h.length>0){s(h,t[3],(n+i)/2,(r+o)/2,i,o)}}this.points=e;this.x=t;this.y=n;this.x1=r;this.y1=i;this.tree={};s(this.points,this.tree,this.x,this.y,this.x1,this.y1)}quadtree.prototype.findNearestPoint=function(e,t){function s(e,t){function f(e,t,n,r,i,s){var o=true;var u;if(i[0]<e&&i[1]<t){u=Math.sqrt(Math.pow(e-i[0],2)+Math.pow(t-i[1],2))}if(i[0]>e&&i[0]<n){if(i[1]<t){u=t-i[1]}if(i[1]>t&&i[1]<r){return o}if(i[1]>r){u=i[1]-r}}if(i[0]>n&&i[1]<t){u=Math.sqrt(Math.pow(n-i[0],2)+Math.pow(t-i[1],2))}if(i[1]>t&&i[1]<r){if(i[0]>n){u=i[0]-n}if(i[0]<e){u=e-i[0]}}if(i[0]>n&&i[1]>r){u=Math.sqrt(Math.pow(n-i[0],2)+Math.pow(r-i[1],2))}if(i[0]<e&&i[1]>r){u=Math.sqrt(Math.pow(e-i[0],2)+Math.pow(r-i[1],2))}if(u>s)o=false;return o}if(e.points){for(var o=0;o<e.points.length;o++){var u=e.points[o];if(String(u)==String(t))continue;var a=Math.sqrt(Math.pow(u[0]-t[0],2)+Math.pow(u[1]-t[1],2));if(a<i||i==-1){i=a;r=u;n=a}}}if(0 in e){if("x"in e[0]){if(f(e[0].x,e[0].y,e[0].x1,e[0].y1,t,n)){s(e[0],t)}}if("x"in e[1]){if(f(e[1].x,e[1].y,e[1].x1,e[1].y1,t,n)){s(e[1],t)}}if("x"in e[2]){if(f(e[2].x,e[2].y,e[2].x1,e[2].y1,t,n)){s(e[2],t)}}if("x"in e[3]){if(f(e[3].x,e[3].y,e[3].x1,e[3].y1,t,n)){s(e[3],t)}}}}var n=t||Math.max(this.x1,this.y1);var r=[];var i=-1;s(this.tree,e);return r}