Skip to content

Instantly share code, notes, and snippets.

@hernanBeiza
Created December 30, 2014 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hernanBeiza/16672747ed367138912d to your computer and use it in GitHub Desktop.
Save hernanBeiza/16672747ed367138912d to your computer and use it in GitHub Desktop.
AS3 - Map
private function map(value:Number, low1:Number, high1:Number, low2:Number = 0, high2:Number = 1):Number {
//El valor, valor min del original, valor máx del original, valor min, valor max
//myBox.x = map(mouseX, 0, 550, 100, 200);v
//http://lab.joelgillman.com/archives/87_map-function
//if the value and the 1st range low are equal to
// the new value must be low2
if (value == low1){
return low2;
}
//normalize both sets to a 0-? range
var range1 = high1 - low1;
var range2 = high2 - low2;
//normalize the value to the new normalized range
var result = value - low1;
//define the range as a percentage (0.0 to 1.0)
var ratio = result / range1;
//find the value in the new normalized-range
result = ratio * range2;
//un-normalize the value in the new range
result += low2;
return result;
}
private function map(v:Number, a:Number, b:Number, x:Number = 0, y:Number = 1):Number {
return (v == a) ? x : (v - a) * (y - x) / (b - a) + x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment