/*******************************************************************************
 * Copyright (c) 2010 by Thomas Jahn
 * Questions or license requests? Mail me at lithander@gmx.de!
 ******************************************************************************/
package net.pixelpracht.geometry 
{
	/**
	 * Provides some Angle related helper methods
	 */
	public class Angle
	{
		public function Angle()
		{
			
		}
		
		public static const PI:Number = Math.PI;
		public static const TwoPI:Number = Math.PI*2;
		
		/**
		* Normalizes angle to be between -PI and +PI.
		*/		
		public static function normalizeRad(angle:Number):Number
		{
			while( angle < -PI)
				angle += TwoPI;
			while( angle > PI)
				angle -= TwoPI;
			return angle;
		}		
		
		/**
		* Normalizes angle to be between -180 and +180.
		*/		
		public static function normalizeDeg(angle:Number):Number
		{
			while( angle < -180)
				angle += 360;
			while( angle > 180)
				angle -= 360;
			return angle;
		}
		
		/**
		* Normalizes angle to be between 0 and 2PI.
		*/		
		public static function normalizeRad2(angle:Number):Number
		{
			while( angle < 0)
				angle += TwoPI;
			while( angle > TwoPI)
				angle -= TwoPI;
			return angle;
		}		
		
		/**
		* Normalizes angle to be between 0 and +360.
		*/		
		public static function normalizeDeg2(angle:Number):Number
		{
			while( angle < 0)
				angle += 360;
			while( angle > 360)
				angle -= 360;
			return angle;
		}
		
		/**
		 * Is an angle in the cone defined by min & max?
		 * Assumes angles are normalized.
		 */		
		public static function isEnclosedNorm(angle:Number, min:Number, max:Number):Boolean
		{
			return (angle > min && angle < max);
		}	
		
		/**
		 * Is an angle (degree) in the cone defined by min & max?
		 */		
		public static function isEnclosedDeg(angle:Number, min:Number, max:Number):Boolean
		{
			while( angle > max)
				angle -= 360;
			while( angle < min)
				angle += 360;
			return (angle > min && angle < max);
		}	
		
		/**
		 * Is a radian angle in the cone defined by min & max?
		 */		
		public static function isEnclosedRad(angle:Number, min:Number, max:Number):Boolean
		{
			while( angle > max)
				angle -= PI;
			while( angle < min)
				angle += PI;
			return (angle > min && angle < max);
		}		
		
		/**
		 * Converts an angle from radian to degrees
		 */		
		public static function radToDeg(radians:Number):Number
		{
			return radians * 180 / Math.PI;
		}
		
		/**
		 * Converts an angle from degree to radian
		 */		
		public static function degToRad(degrees:Number):Number
		{
			return degrees * Math.PI / 180;
		}	
		
		/**
		 * Returns cone angle in radian with (0,0) as center
		 */	
		public static function coneAngle(v:Vector2D, v2:Vector2D):Number
		{
			return Math.atan2(v.y,v.x) - Math.atan2(v2.y,v2.x);
		}
		
		/**
		 * Returns polar angle in radian
		 */	
		public static function polarAngle(v:Vector2D):Number
		{
			return Math.atan2(v.y, v.x);
		}

	}
}