Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Created February 27, 2014 16:56
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 alexanderjeurissen/9254223 to your computer and use it in GitHub Desktop.
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
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