Skip to content

Commit

Permalink
Add rubberBand #102
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed May 7, 2022
1 parent 5a29bd9 commit 5b053b6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/FluidInterfaceKit/Helper/Fluid.swift
Expand Up @@ -166,5 +166,34 @@ public enum Fluid {
return "<\(values.joined(separator: "; "))>"
}
}

/// https://gist.githubusercontent.com/jverkoey/3955471c6670386c54e1d62d9c965b53/raw/b618a3da1dab454195fd9ad66323cf7f36c93639/rubberBand.swift
public static func rubberBand(value: CGFloat, min: CGFloat, max: CGFloat, bandLength: CGFloat) -> CGFloat {
if value >= min && value <= max {
// While we're within range we don't rubber band the value.
return value
}

if bandLength <= 0 {
// The rubber band doesn't exist, return the minimum value so that we stay put.
return min
}

let rubberBandCoefficient: CGFloat = 0.55
// Accepts values from [0...+inf and ensures that f(x) < bandLength for all values.
let band: (CGFloat) -> CGFloat = { value in
let demoninator = value * rubberBandCoefficient / bandLength + 1
return bandLength * (1 - 1 / demoninator)
}
if (value > max) {
return band(value - max) + max;

} else if (value < min) {
return min - band(min - value);
}

return value;
}


}

0 comments on commit 5b053b6

Please sign in to comment.