Created
May 26, 2018 15:29
-
-
Save khanlou/f0f1a860004d4e97b21f0f0e9309ceca to your computer and use it in GitHub Desktop.
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
public extension BinaryFloatingPoint { | |
public func scaled(from source: ReversibleRange<Self>, to destination: ReversibleRange<Self>) -> Self { | |
let destinationStart = destination.lowerBound | |
let destinationEnd = destination.upperBound | |
let selfMinusLower = self - source.lowerBound | |
let sourceUpperMinusLower = source.upperBound - source.lowerBound | |
let destinationUpperMinusLower = destinationEnd - destinationStart | |
let percentThroughSource = (selfMinusLower / sourceUpperMinusLower) | |
let curvedPercent = percentThroughSource | |
return curvedPercent * destinationUpperMinusLower + destinationStart | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment