Skip to content

Instantly share code, notes, and snippets.

@whymarrh
Created March 12, 2017 23:08
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 whymarrh/995da3abc316659d43ba755f97ea6ebd to your computer and use it in GitHub Desktop.
Save whymarrh/995da3abc316659d43ba755f97ea6ebd to your computer and use it in GitHub Desktop.
Map a value from one range to another
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