Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.