/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */
//>>built
define("esri/geometry/Polygon",["dojo/_base/declare","dojo/_base/array","dojo/_base/lang","dojo/has","esri/kernel","esri/lang","esri/SpatialReference","esri/geometry/Geometry","esri/geometry/Point","esri/geometry/Extent","esri/geometry/mathUtils"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b){var _c={type:"polygon",rings:null};var _d=_1(_8,{declaredClass:"esri.geometry.Polygon",constructor:function(_e){_3.mixin(this,_c);this.rings=[];this._ring=0;if(_e){if(_e.rings){_3.mixin(this,_e);}else{this.spatialReference=_e;}if(this.spatialReference){this.spatialReference=new _7(this.spatialReference);}}this.verifySR();},_extent:null,addRing:function(_f){this._extent=null;this._ring=this.rings.length;this.rings[this._ring]=[];if(_3.isArray(_f[0])){_2.forEach(_f,this._addPointArr,this);}else{_2.forEach(_f,this._addPoint,this);}return this;},_addPointArr:function(_10){this.rings[this._ring].push(_10);},_addPoint:function(_11){this.rings[this._ring].push([_11.x,_11.y]);},_insertPoints:function(_12,_13){this._extent=null;this._ring=_13;if(!this.rings[this._ring]){this.rings[this._ring]=[];}_2.forEach(_12,this._addPoint,this);},_validateInputs:function(_14,_15){if((_14!==null&&_14!==undefined)&&(_14<0||_14>=this.rings.length)){return false;}if((_15!==null&&_14!==undefined)&&(_15<0||_15>=this.rings[_14].length)){return false;}return true;},getPoint:function(_16,_17){if(this._validateInputs(_16,_17)){return new _9(this.rings[_16][_17],this.spatialReference);}},setPoint:function(_18,_19,_1a){if(this._validateInputs(_18,_19)){this._extent=null;this.rings[_18][_19]=[_1a.x,_1a.y];return this;}},insertPoint:function(_1b,_1c,_1d){if(this._validateInputs(_1b)&&_6.isDefined(_1c)&&(_1c>=0&&_1c<=this.rings[_1b].length)){this._extent=null;this.rings[_1b].splice(_1c,0,[_1d.x,_1d.y]);return this;}},removeRing:function(_1e){if(this._validateInputs(_1e,null)){this._extent=null;var arr=this.rings.splice(_1e,1)[0],i,il=arr.length,sr=this.spatialReference;for(i=0;i<il;i++){arr[i]=new _9(arr[i],sr);}return arr;}},removePoint:function(_1f,_20){if(this._validateInputs(_1f,_20)){this._extent=null;return new _9(this.rings[_1f].splice(_20,1)[0],this.spatialReference);}},getExtent:function(){var _21;if(this._extent){_21=new _a(this._extent);_21._partwise=this._partwise;return _21;}var _22=this.rings,pal=_22.length;if(!pal||!_22[0].length){return;}var _23,_24,x,y,_25,_26,pa,pt,ptl,_27=(_25=_22[0][0][0]),_28=(_26=_22[0][0][1]),min=Math.min,max=Math.max,sr=this.spatialReference,_29=[],_2a,_2b,_2c,_2d;for(pa=0;pa<pal;pa++){_23=_22[pa];_2a=(_2b=_23[0]&&_23[0][0]);_2c=(_2d=_23[0]&&_23[0][1]);ptl=_23.length;for(pt=0;pt<ptl;pt++){_24=_23[pt];x=_24[0];y=_24[1];_27=min(_27,x);_28=min(_28,y);_25=max(_25,x);_26=max(_26,y);_2a=min(_2a,x);_2c=min(_2c,y);_2b=max(_2b,x);_2d=max(_2d,y);}_29.push(new _a({xmin:_2a,ymin:_2c,xmax:_2b,ymax:_2d,spatialReference:(sr?sr.toJson():null)}));}this._extent={xmin:_27,ymin:_28,xmax:_25,ymax:_26,spatialReference:(sr?sr.toJson():null)};this._partwise=_29.length>1?_29:null;_21=new _a(this._extent);_21._partwise=this._partwise;return _21;},contains:function(_2e){var _2f=this.rings,_30,_31=false,pi,pj,_32,j,i,pa,pal=_2f.length,_33=this.spatialReference,_34=_2e.spatialReference,_35,x=_2e.x,y=_2e.y;if(_33&&_34&&!_33.equals(_34)&&_33._canProject(_34)){_35=_33.isWebMercator()?_9.lngLatToXY(x,y):_9.xyToLngLat(x,y,true);x=_35[0];y=_35[1];}for(pa=0;pa<pal;pa++){_30=_2f[pa];_32=_30.length;j=0;for(i=0;i<_32;i++){j++;if(j===_32){j=0;}pi=_30[i];pj=_30[j];if((pi[1]<y&&pj[1]>=y||pj[1]<y&&pi[1]>=y)&&(pi[0]+(y-pi[1])/(pj[1]-pi[1])*(pj[0]-pi[0])<x)){_31=!_31;}}}return _31;},isClockwise:function(arr){var _36=0,i,il=arr.length,_37=_3.isArray(arr[0])?function(p1,p2){return p1[0]*p2[1]-p2[0]*p1[1];}:function(p1,p2){return p1.x*p2.y-p2.x*p1.y;};for(i=0;i<il;i++){_36+=_37(arr[i],arr[(i+1)%il]);}return (_36/2)<=0;},isSelfIntersecting:function(_38){_38=_38||this;var i,j,k,m,_39,_3a,_3b,_3c=_38.rings.length;for(k=0;k<_3c;k++){for(i=0;i<_38.rings[k].length-1;i++){_39=[[_38.rings[k][i][0],_38.rings[k][i][1]],[_38.rings[k][i+1][0],_38.rings[k][i+1][1]]];for(j=k+1;j<_3c;j++){for(m=0;m<_38.rings[j].length-1;m++){_3a=[[_38.rings[j][m][0],_38.rings[j][m][1]],[_38.rings[j][m+1][0],_38.rings[j][m+1][1]]];_3b=_b._getLineIntersection2(_39,_3a);if(_3b){if(!((_3b[0]===_39[0][0]&&_3b[1]===_39[0][1])||(_3b[0]===_3a[0][0]&&_3b[1]===_3a[0][1])||(_3b[0]===_39[1][0]&&_3b[1]===_39[1][1])||(_3b[0]===_3a[1][0]&&_3b[1]===_3a[1][1]))){return true;}}}}}var _3d=_38.rings[k].length;if(_3d<=4){continue;}for(i=0;i<_3d-3;i++){var _3e=_3d-1;if(i===0){_3e=_3d-2;}_39=[[_38.rings[k][i][0],_38.rings[k][i][1]],[_38.rings[k][i+1][0],_38.rings[k][i+1][1]]];for(j=i+2;j<_3e;j++){_3a=[[_38.rings[k][j][0],_38.rings[k][j][1]],[_38.rings[k][j+1][0],_38.rings[k][j+1][1]]];_3b=_b._getLineIntersection2(_39,_3a);if(_3b){if(!((_3b[0]===_39[0][0]&&_3b[1]===_39[0][1])||(_3b[0]===_3a[0][0]&&_3b[1]===_3a[0][1])||(_3b[0]===_39[1][0]&&_3b[1]===_39[1][1])||(_3b[0]===_3a[1][0]&&_3b[1]===_3a[1][1]))){return true;}}}}}return false;},toJson:function(){var _3f={rings:_3.clone(this.rings)},sr=this.spatialReference;if(sr){_3f.spatialReference=sr.toJson();}return _3f;}});_d.defaultProps=_c;_d.createEllipse=function(_40){var dx=_40.center.x,dy=_40.center.y,a=_40.longAxis,b=_40.shortAxis,_41=_40.numberOfPoints,map=_40.map,pt,i,_42,_43,_44,_45=[],_46=(2*Math.PI)/_41;for(i=0;i<_41;i++){_42=Math.cos(i*_46);_43=Math.sin(i*_46);pt=map.toMap({x:a*_42+dx,y:b*_43+dy});_45.push(pt);}_45.push(_45[0]);_44=new _d(map.spatialReference);_44.addRing(_45);return _44;};_d.createCircle=function(_47){var _48={center:_47.center,longAxis:_47.r,shortAxis:_47.r,numberOfPoints:_47.numberOfPoints,map:_47.map};var _49=_d.createEllipse(_48);return _49;};if(_4("extend-esri")){_3.setObject("geometry.Polygon",_d,_5);_5.geometry.defaultPolygon=_c;_5.geometry.createEllipse=_d.createEllipse;_5.geometry.createCircle=_d.createCircle;_5.geometry.isClockwise=_d.prototype.isClockwise;_5.geometry.polygonSelfIntersecting=_d.prototype.isSelfIntersecting;}return _d;});