Last active
November 26, 2015 06:35
-
-
Save branneman/4e976ff88154b7fbf5e0 to your computer and use it in GitHub Desktop.
Map a value in a range to another range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Map a value in a domain to a value in a range | |
*/ | |
function scale(domainMin, domainMax, value, rangeMin, rangeMax) { | |
var perc = (value - domainMin) / (domainMax - domainMin) | |
return ((rangeMax - rangeMin) * perc) + rangeMin | |
} | |
scale(50, 100, 75, 100, 200) //=> 150 | |
scale(10, 110, 50, 200, 300) //=> 240 | |
scale(-50, 0, -25, -100, -200) //=> -150 | |
scale(100, 150, 200, 10, 20) //=> 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment