Posted By

adrianparr on 06/13/12


Tagged

number math map Value as3 percent range percerttovalue valuetopercent


Versions (?)

AS3 Map a Number within one range to another range


 / Published in: ActionScript 3
 

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

Credit goes to Bruno Imbrizi.

  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. }

Report this snippet  

You need to login to post a comment.