Last active
November 30, 2017 00:23
-
-
Save mkremins/32698e34f885a38bda3a to your computer and use it in GitHub Desktop.
Convert number from one scale to another
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
(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))) |
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
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