Last active
May 11, 2017 15:03
-
-
Save shortcircuit3/38328fef79d74f95c70933a977bd3586 to your computer and use it in GitHub Desktop.
A function to translate a number between two different ranges
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
// 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