/**
 * Graham's Scan Convex Hull Algorithm
 * @desc An implementation of the Graham's Scan Convex Hull algorithm in JavaScript.
 * @author Brian Barnett, brian@3kb.co.uk, http://brianbar.net/ || http://3kb.co.uk/
 * @author Roman Rubsamen, roman.rubsamen@gmail.com
 * @version 1.0
 */
function ConvexHullGrahamScan(){this.anchorPoint=void 0,this.reverse=!1,this.points=[]}ConvexHullGrahamScan.prototype={constructor:ConvexHullGrahamScan,Point:function(a,b,c){this.x=a,this.y=b,this.name=c},_findPolarAngle:function(a,b){var c=57.295779513082,d=b.x-a.x,e=b.y-a.y;if(0==d&&0==e)return 0;var f=Math.atan2(e,d)*c;return this.reverse?0>=f&&(f+=360):f>=0&&(f+=360),f},addPoint:function(a,b,c){return void 0===this.anchorPoint?void(this.anchorPoint=new this.Point(a,b,c)):this.anchorPoint.y>b&&this.anchorPoint.x>a||this.anchorPoint.y===b&&this.anchorPoint.x>a||this.anchorPoint.y>b&&this.anchorPoint.x===a?(this.points.push(new this.Point(this.anchorPoint.x,this.anchorPoint.y,this.anchorPoint.name)),void(this.anchorPoint=new this.Point(a,b,c))):void this.points.push(new this.Point(a,b,c))},_sortPoints:function(){var a=this;return this.points.sort(function(b,c){var d=a._findPolarAngle(a.anchorPoint,b),e=a._findPolarAngle(a.anchorPoint,c);return e>d?-1:d>e?1:0})},_checkPoints:function(a,b,c){var d,e=this._findPolarAngle(a,b),f=this._findPolarAngle(a,c);return e>f?(d=e-f,!(d>180)):f>e?(d=f-e,d>180):!0},getHull:function(){var a,b,c=[];if(this.reverse=this.points.every(function(a){return a.x<0&&a.y<0}),a=this._sortPoints(),b=a.length,4>b)return a.unshift(this.anchorPoint),a;for(c.push(a.shift(),a.shift());;){var d,e,f;if(c.push(a.shift()),d=c[c.length-3],e=c[c.length-2],f=c[c.length-1],this._checkPoints(d,e,f)&&c.splice(c.length-2,1),0==a.length){if(b==c.length){var g=this.anchorPoint;return c.some(function(a){return a.x==g.x&&a.y==g.y})||c.unshift(this.anchorPoint),c}a=c,b=a.length,c=[],c.push(a.shift(),a.shift())}}}},"function"==typeof define&&define.amd&&define(function(){return ConvexHullGrahamScan}),"undefined"!=typeof module&&(module.exports=ConvexHullGrahamScan);