Skip to content

Instantly share code, notes, and snippets.

@shortcircuit3
Last active May 11, 2017 15:03
Show Gist options
  • Save shortcircuit3/38328fef79d74f95c70933a977bd3586 to your computer and use it in GitHub Desktop.
Save shortcircuit3/38328fef79d74f95c70933a977bd3586 to your computer and use it in GitHub Desktop.
A function to translate a number between two different ranges
// convertValue()
// VALUE
// The number to input into the equation.
// To consider: What will the starting and ending value be?
//
// R1MIN
// The starting input range. What is the lowest number that VALUE will have?
//
// R1MAX
// The top of the input range. What is the highest number that VALUE will have?
//
// R2MIN
// The bottom value of the target range.
//
// R2MAX
// The top value of the target range
//
// RETURN
// The number that falls somewhere inbetween r2Min and r2Max
func convertValue(value: CGFloat, r1Min: CGFloat, r1Max: CGFloat, r2Min: CGFloat, r2Max: CGFloat) -> CGFloat {
let ratio = (r2Max - r2Min) / (r1Max - r1Min)
return value * ratio + r2Min - r1Min * ratio
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment