package geom 
{
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author RedMaster
	 */
	public class Polygon2D 
	{
		protected var _geometry:Vector.<Point> = new Vector.<Point>();
		
		public function Polygon2D(geometry:Vector.<Point> = null) 
		{
			if (geometry != null) _geometry = geometry;
		}
		
		public function offset(x:Number, y:Number):void
		{
			for (var i:int = 0; i < _geometry.length; i++)
			{
				var p:Point = _geometry[i];
				p.x += x;
				p.y += y;
			}
		}
		
		public function addPoint(point:Point):void
		{
			_geometry.push(point);
		}
		public function get pointsQuantity():uint
		{
			return _geometry.length;
		}
		public function getPoint(index:uint):Point
		{
			try
			{
				return _geometry[index];
			}
			catch (e:Error)
			{
				throw e;
			}
			return null;
		}
		public function setPoint(index:uint, point:Point):void
		{
			try
			{
				_geometry[index] = point;
			}
			catch (e:Error)
			{
				throw e;
			}
		}
		public function clearPoints():void
		{
			_geometry = null;
			_geometry = new Vector.<Point>();
		}
		public function get center():Point
		{
			var cx:Number = 0;
			var cy:Number = 0;
			for (var i:int = 0; i < pointsQuantity; i++)
			{
				cx += getPoint(i).x;
				cy += getPoint(i).y;
			}
			cx /= pointsQuantity;
			cy /= pointsQuantity;
			return new Point(cx, cy);
		}
		
		public function clone():Polygon2D
		{
			var result:Polygon2D = new Polygon2D();
			for (var i:uint = 0; i < _geometry.length; i++)
			{
				result.addPoint(_geometry[i].clone());
			}
			return result;
		}
	}
}