Created
February 27, 2014 16:56
-
-
Save alexanderjeurissen/9254223 to your computer and use it in GitHub Desktop.
This function scales the given value from one domain to another. scale( [a,b] , [min,max] , a ) = min AND scale( [a,b] , [min,max] , b ) = max
This file contains 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
var scale = function (sourceDomain, targetDomain, x) { | |
var sourceMin = sourceDomain[0]; | |
var sourceMax = sourceDomain[1]; | |
var targetMin = targetDomain[0]; | |
var targetMax = targetDomain[1]; | |
return { | |
value: (((targetMax - targetMin) * (x - sourceMin)) / (sourceMax - sourceMin)) + targetMin, | |
scalingFactor: (targetMax - targetMin) / (sourceMax - sourceMin) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment