Skip to content

Instantly share code, notes, and snippets.

@mkremins
Last active November 30, 2017 00:23
Show Gist options
  • Save mkremins/32698e34f885a38bda3a to your computer and use it in GitHub Desktop.
Save mkremins/32698e34f885a38bda3a to your computer and use it in GitHub Desktop.
Convert number from one scale to another
(defn scale
"Converts the number `x` from the scale `[old-min old-max]` to the scale
`[new-min new-max]`."
[x [old-min old-max] [new-min new-max]]
(let [old-range (- old-max old-min)
new-range (- new-max new-min)]
(+ (/ (* (- x old-min) new-range) old-range) new-min)))
function scale(x, oldMin, oldMax, newMin, newMax){
let oldRange = oldMax - oldMin;
let newRange = newMax - newMin;
return ((newRange * (x - oldMin)) / oldRange) + newMin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment