Skip to content

Instantly share code, notes, and snippets.

@n8agrin
Created March 22, 2013 22:26
Show Gist options
  • Save n8agrin/5225249 to your computer and use it in GitHub Desktop.
Save n8agrin/5225249 to your computer and use it in GitHub Desktop.
Simplified linear scale based on d3.
function uninterpolate (a, b, x) {
b = b - (a = +a) ? 1 / (b - a) : 0;
return (x - a) * b;
}
function interpolate (a, b, t) {
return (a + (b - a)) * t;
}
// Given a domain [0, 10], a range [0, 100], and a value (v),
// return the scaled output.
function linearscale (domain, range, v) {
var u = uninterpolate(domain[0], domain[1], v);
var i = interpolate(range[0], range[1], u);
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment