Return to Snippet

Revision: 57864
at June 13, 2012 02:56 by adrianparr


Initial Code
package com.brunoimbrizi.utils {
	
	/**
	 * Class		MathUtils
	 * 
	 * @author 		Bruno Imbrizi
	 */
	
	public class MathUtils {
		
		
		/**
		 * Returns a number constrained between min and max.
		 * @param	num
		 * @param	min
		 * @param	max
		 * @return
		 */
		public static function constrain(num:Number, min:Number = 0, max:Number = 1):Number
		{
			if (num < min) return min;
			if (num > max) return max;
			return num;
		}
		
		
		/**
		 * Returns a random number between min and max.
		 * 
		 * @param	min
		 * @param	max
		 * @return
		 */
		public static function randomRange(min:Number, max:Number, round:Boolean = false):Number 
		{
			var num:Number = (min + Math.random() * (max - min));
			
			if (round) return Math.round(num);
			return num;
		}
		
		
		/**
		 * Returns the percentage of a number in a given range.
		 * Example: num = 15 range 10 to 20 // outputs 0.5
		 * 
		 * @param	num
		 * @param	min
		 * @param	max
		 * @param	constrainMin		Returns 0 if num < min.
		 * @param	constrainMax		Returns 1 if num > max.
		 * @return
		 */
		public static function rangeToPercent(num:Number, min:Number, max:Number, constrainMin:Boolean = false, constrainMax:Boolean = false):Number 
		{
			if (constrainMin && num < min) return 0;
			if (constrainMax && num > max) return 1;
			return (num - min) / (max - min);
		}

		
		/**
		 * Returns the number that corresponds to the percentage in a given range.
		 * Example: percent = 0.5 range 10 to 20 // outputs 15
		 * 
		 * @param	percent
		 * @param	min
		 * @param	max
		 * @return
		 */
		public static function percentToRange(percent:Number, min:Number, max:Number):Number 
		{
			return (percent * (max - min)) + min;
		}
		
		
		/**
		 * Re-maps a number from one range to another. The output is the same as inputing the result of rangeToPercent() into percentToRange().
		 * Example: num = 10, min1 = 0, max1 = 100, min2 = 0, max2 = 50 // outputs 5
		 * @param	num
		 * @param	min1
		 * @param	max1
		 * @param	min2
		 * @param	max2
		 * @return
		 */
		public static function map(num:Number, min1:Number, max1:Number, min2:Number, max2:Number, round:Boolean = true, constrainMin:Boolean = true, constrainMax:Boolean = true):Number
		{
			if (constrainMin && num < min1) return min2;
			if (constrainMax && num > max1) return max2;
			
			var num1:Number = (num - min1) / (max1 - min1);
			var num2:Number = (num1 * (max2 - min2)) + min2;
			if (round) return Math.round(num2);
			return num2;
		}
		
		
		/**
		 * Converts radians to degrees.
		 * 
		 * @param	radians
		 * @return
		 */
		public static function radiansToDegrees(radians:Number):Number 
		{
			return (radians * 180 / Math.PI);
		}
		
		
		/**
		 * Converts degrees to radians.
		 * 
		 * @param	degrees
		 * @return
		 */ 
		public static function degreesToRadians(degrees:Number):Number 
		{
			return (degrees * Math.PI / 180);
		}
		
		
		/**
		* Returns 1 if the value is >= 0. Returns -1 if the value is < 0.
		* 
		* @param	num
		* @return
		*/     
		public static function sign(num:Number):int {
			if (num < 0) return -1
			return 1;
		}
	}
	
	
}

Initial URL
http://brunoimbrizi.com/unbox/wp-content/uploads/2011/03/MathUtils.as

Initial Description
Credit goes to Bruno Imbrizi.

Initial Title
AS3 MathUtils static class

Initial Tags
class, number, math

Initial Language
ActionScript 3