Skip to content

Instantly share code, notes, and snippets.

@nubbel
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nubbel/ec66832e2fe5e6f4b055 to your computer and use it in GitHub Desktop.
Save nubbel/ec66832e2fe5e6f4b055 to your computer and use it in GitHub Desktop.
// Interpol.playground
import CoreGraphics
typealias Time = CFTimeInterval
typealias Value = CGFloat
typealias Point = CGPoint
typealias Vector = CGVector
typealias Size = CGSize
typealias Rect = CGRect
struct Interpolator<T> {
typealias Interpolation = Time -> T
let interpolation: Interpolation
init(_ interpolation: Interpolation) {
self.interpolation = interpolation
}
func interpolate(t: Time) -> T {
return interpolation(t)
}
}
extension Interpolator {
subscript(t: Time) -> T {
return interpolate(t)
}
}
func interpolation(from: Value, to: Value) -> Interpolator<Value> {
return Interpolator { t in
Value(1 - t) * from + Value(t) * to
}
}
func interpolation(from: Point, to: Point) -> Interpolator<Point> {
let interpolator = (
x: interpolation(from.x, to.x),
y: interpolation(from.y, to.y)
)
return Interpolator { t in
Point(x: interpolator.x[t], y: interpolator.y[t])
}
}
func interpolation(from: Vector, to: Vector) -> Interpolator<Vector> {
let interpolator = (
dx: interpolation(from.dx, to.dx),
dy: interpolation(from.dy, to.dy)
)
return Interpolator { t in
Vector(dx: interpolator.dx[t], dy: interpolator.dy[t])
}
}
func interpolation(from: Size, to: Size) -> Interpolator<Size> {
let interpolator = (
width: interpolation(from.width, to.width),
height: interpolation(from.height, to.height)
)
return Interpolator { t in
Size(width: interpolator.width[t], height: interpolator.height[t])
}
}
func interpolation(from: Rect, to: Rect) -> Interpolator<Rect> {
let interpolator = (
origin: interpolation(from.origin, to.origin),
size: interpolation(from.size, to.size)
)
return Interpolator { t in
Rect(origin: interpolator.origin[t], size: interpolator.size[t])
}
}
// Examples
let valueInterpolation = interpolation(0, 100)
valueInterpolation.interpolate(0.0) // => 0.0
valueInterpolation.interpolate(0.5) // => 50.0
valueInterpolation.interpolate(1.0) // => 100.0
let pointInterpolation = interpolation(CGPointZero, CGPoint(x: 100, y: 200))
pointInterpolation.interpolate(0.0) // => ( 0.0, 0.0)
pointInterpolation.interpolate(0.5) // => ( 50.0, 100.0)
pointInterpolation.interpolate(1.0) // => (100.0, 200.0)
let vectorInterpolation = interpolation(CGVector(dx: 0, dy: 0), CGVector(dx: 100, dy: 200))
vectorInterpolation.interpolate(0.0) // => ( 0.0, 0.0)
vectorInterpolation.interpolate(0.5) // => ( 50.0, 100.0)
vectorInterpolation.interpolate(1.0) // => (100.0, 200.0)
let sizeInterpolation = interpolation(CGSizeZero, CGSize(width: 100, height: 200))
sizeInterpolation.interpolate(0.0) // => ( 0.0, 0.0)
sizeInterpolation.interpolate(0.5) // => ( 50.0, 100.0)
sizeInterpolation.interpolate(1.0) // => (100.0, 200.0)
let rectInterpolation = interpolation(CGRectZero, CGRect(x: 500, y: 500, width: 100, height: 200))
rectInterpolation.interpolate(0.0) // => ( 0.0, 0.0, 0.0, 0.0)
rectInterpolation.interpolate(0.5) // => (250.0, 250.0, 50.0, 100.0)
rectInterpolation.interpolate(1.0) // => (500.0, 500.0, 100.0, 200.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment