Skip to content

Instantly share code, notes, and snippets.

@mcichecki
Last active July 17, 2024 09:38
Show Gist options
  • Save mcichecki/bf8808f123ce342b17058abcca3b5378 to your computer and use it in GitHub Desktop.
Save mcichecki/bf8808f123ce342b17058abcca3b5378 to your computer and use it in GitHub Desktop.
Normalized value of CGFloat between two values
extension BinaryFloatingPoint {
/// Returns normalized value for the range between `a` and `b`
/// - Parameters:
/// - min: minimum of the range of the measurement
/// - max: maximum of the range of the measurement
/// - a: minimum of the range of the scale
/// - b: minimum of the range of the scale
func normalize(min: Self, max: Self, from a: Self = 0, to b: Self = 1) -> Self {
(b - a) * ((self - min) / (max - min)) + a
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment