Return to Snippet

Revision: 57863
at June 13, 2012 02:53 by adrianparr


Initial Code
public static function map(num:Number, min1:Number, max1:Number, min2:Number, max2:Number, round:Boolean = false, 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;
}

Initial URL
http://brunoimbrizi.com/unbox/2011/03/mathutils-map/

Initial Description
Credit goes to Bruno Imbrizi.

Initial Title
AS3 Map a Number within one range to another range

Initial Tags
number, math

Initial Language
ActionScript 3