Created
March 12, 2017 23:08
-
-
Save whymarrh/995da3abc316659d43ba755f97ea6ebd to your computer and use it in GitHub Desktop.
Map a value from one range 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
fun rangeMap(value: Double, from: ClosedRange<Double>, to: ClosedRange<Double>): Double { | |
require(value in from, { "value $value must be in source range $from" }) | |
return to.start + (to.endInclusive - to.start) * (value - from.start) / (from.endInclusive - from.start) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment