AS3 Map a Number within one range to another range


/ Published in: ActionScript 3
Save to your folder(s)

Credit goes to Bruno Imbrizi.


Copy this code and paste it in your HTML
  1. public static function map(num:Number, min1:Number, max1:Number, min2:Number, max2:Number, round:Boolean = false, constrainMin:Boolean = true, constrainMax:Boolean = true):Number
  2. {
  3. if (constrainMin && num < min1) return min2;
  4. if (constrainMax && num > max1) return max2;
  5.  
  6. var num1:Number = (num - min1) / (max1 - min1);
  7. var num2:Number = (num1 * (max2 - min2)) + min2;
  8. if (round) return Math.round(num2);
  9. return num2;
  10. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.