-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeotrouvetou.min.js
22 lines (18 loc) · 3.37 KB
/
geotrouvetou.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// The MIT License (MIT)
// Copyright (c) 2013 Jean-Baptiste Pin
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
function GeoPoint(t,e){this.latitude=t,this.longitude=e,this.data=null}function TreeNode(t){this.point=t,this.leaf=[]}function GeoTrouvetou(){this.tree=null}GeoPoint.prototype.getDirection=function(t){var e=0;return this.latitude>t.latitude&&(e=2),this.longitude>t.longitude&&(e+=1),e},GeoPoint.prototype.equals=function(t){return t.latitude==this.latitude&&t.longitude==this.longitude?!0:void 0},GeoPoint.prototype.closer=function(t,e){var i=Math.pow(t.latitude-this.latitude,2)+Math.pow(t.longitude-this.longitude,2),o=Math.pow(e.latitude-this.latitude,2)+Math.pow(e.longitude-this.longitude,2);return o>i?t:e},TreeNode.prototype.addLeaf=function(t){var e=this.find(t),i=e.point.getDirection(t);if(e.leaf[i]){var o=e.point.closer(t,e.leaf[i].point);if(o==e.leaf[i].point)e.leaf[i].addLeaf(t);else{var n=e.leaf[i],r=new TreeNode(t);r.leaf[r.point.getDirection(n.point)]=n,e.leaf[i]=r}}else e.leaf[i]=new TreeNode(t)},TreeNode.prototype.logPoint=function(t){console.log(t.latitude+" "+t.longitude)},TreeNode.prototype.find=function(t,e){var i=this.point.getDirection(t);if(this.leaf[i]){var o=this.leaf[i].find(t,e),n=t.closer(o.point,this.point);n==this.point&&(o=this);var r,l,a;if(0===i||3===i?(this.leaf[1]&&(l=this.leaf[1].find(t,e)),this.leaf[2]&&(a=this.leaf[2].find(t,e))):(this.leaf[0]&&(l=this.leaf[0].find(t,e)),this.leaf[3]&&(a=this.leaf[3].find(t,e))),l&&a)n=t.closer(l.point,a.point),r=n==l.point?l:a;else if(l)r=l;else{if(!a)return o;r=a}return n=t.closer(o.point,r.point),n==r.point&&(o=r),o}if(this.leaf.length){var u=this;return this.leaf.forEach(function(i){var o=i.point;e&&(o=i.find(t,e));var n=t.closer(u.point,i.point);n!==u.point&&(u=i)}),u==this?this:u.find(t,e)}return this},TreeNode.prototype.toString=function(t){t||(t="."),this.point.data&&this.point.data.name&&console.log(t+" "+this.point.data.name+": "+this.point.latitude+","+this.point.longitude),this.leaf.forEach(function(e,i){e.toString(t+"--"+i)})},GeoTrouvetou.prototype.addPoint=function(t){this.tree?this.tree.addLeaf(t):this.tree=new TreeNode(t)},GeoTrouvetou.prototype.findClosest=function(t){return null===this.tree?null:this.tree.find(t).point},GeoTrouvetou.prototype.toString=function(){this.tree.toString()},"undefined"!=typeof module&&module.exports&&(module.exports={GeoTrouvetou:GeoTrouvetou,GeoPoint:GeoPoint});