Skip to content

Instantly share code, notes, and snippets.

@jcmuller
Created April 17, 2014 13:30
Show Gist options
  • Save jcmuller/10983596 to your computer and use it in GitHub Desktop.
Save jcmuller/10983596 to your computer and use it in GitHub Desktop.
Scale Function
scale = (inMin, inMax, outMin, outMax) ->
inMax = parseFloat(inMax)
inMin = parseFloat(inMin)
outMin = parseFloat(outMin)
outMax = parseFloat(outMax)
inRange = inMax - inMin
outRange = outMax - outMin
inMultiplier = 1 / inRange
outMultiplier = 1 / outRange
(x) -> (x - inMin) * inMultiplier / outMultiplier + outMin
var scale = function(inMin, inMax, outMin, outMax) {
var inMultiplier, inRange, outMultiplier, outRange;
inMax = parseFloat(inMax);
inMin = parseFloat(inMin);
outMin = parseFloat(outMin);
outMax = parseFloat(outMax);
inRange = inMax - inMin;
outRange = outMax - outMin;
inMultiplier = 1 / inRange;
outMultiplier = 1 / outRange;
return function(x) {
return (x - inMin) * inMultiplier / outMultiplier + outMin;
};
};
@jcmuller
Copy link
Author

Scaling functions are very useful when you want to map a value from one domain into a value on another.

Example:

> fahrToCelciusScale = scale(32, 212, 0, 100)
> fahrToCelciusScale(85)
29.444444444444446

> fahrToCelciusScale(-40)
-40 # Wait, what?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment