package  
{
	import flash.geom.Point;
	import flash.utils.Proxy;
	/**
	 * ...
	 * @author beisik
	 */
	public class Global 
	{		
		public function Global();
		
		public static function Aspire(s: Number, aim: Number, speed: Number): Number
		{
			var b: Boolean = true;
			if (s + speed < aim) { s += speed; b = false; }
			if (s - speed > aim) { s -= speed; b = false; }
			if (b) s = aim;
			return s;
		}
		
		public static function AspireDegrees(s: Number, aim: Number, speed: Number): Number
		{
			//return Aspire(s, aim, speed);
			//var a: Number;
			if (Math.abs(aim - s) > 180)
			{
				if (s > aim)
					aim +=360;
				else
					aim -= 360;
			}
			return Aspire(s, aim, speed);
		}
		
		public static function collideCircleRect(x: Number, y: Number, radius: Number, rcx: Number, rcy: Number, rhw: Number, rhh: Number): Boolean
		{
			return ((x + radius >= rcx - rhw) && (x - radius <= rcx + rhw) && (y + radius >= rcy - rhh) && (y - radius <= rcy + rhh));
		}
		
		public static function Distance(x1: Number, y1: Number, x2: Number, y2: Number): Number
		{
			return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
		}
		
		public static function Speed(x1: Number, y1: Number, x2: Number, y2: Number): Point
		{
			var x: Number = x2 - x1, y: Number = y2 - y1;
			var l: Number = Math.sqrt(x * x + y * y);
			if (l == 0)
				return new Point(0, 0)
			else
				return new Point(x / l, y / l);
		}
		
		// в радианах!!!
		public static function Angle2D(p1x: Number, p1y: Number, p2x: Number, p2y: Number): Number
		{
			if (p1x == p2x)
			{
				if (p1y == p2y)
					return 0;
				/*if (p1y < p2y)
					return Math.PI / 2;
				else
					return - Math.PI / 2;*/
			}
			var ang: Number = Math.atan((p1y - p2y) / (p1x - p2x));
			if ((p1x >= p2x) && (p1y >= p2y))
				return ang;
			if ((p1x < p2x) && (p1y > p2y))
				return Math.PI + ang;
			if ((p1x < p2x) && (p1y <= p2y))
				return Math.PI + ang;
			if ((p1x >= p2x) && (p1y < p2y))
				return Math.PI * 2 + ang;
			return 0;
		}
		
		public static function normalDegrees(deg: Number): Number
		{
			while (deg >= 360)
				deg = deg - 360;
			while (deg < 0)
				deg = deg + 360;
				
			if (deg > 180)
				deg -= 360;			
			
			return deg;
		}
		
		public static function sign(v1: Number, v2: Number): Number
		{
			if (v1 < v2)
				return 1;
			if (v1 > v2)
				return -1;
			return 0;
		}
	}

}