Created
November 8, 2015 13:53
-
-
Save carlosefonseca/6299b58cc7166a333c6c to your computer and use it in GitHub Desktop.
Port of https://bitbucket.org/kluivers/jk-interpolation to Swift.
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
// | |
// JKInterpolationMath.h | |
// | |
// Created by Carlos Fonseca on 08/11/15. | |
// Copyright © 2015 Carlos Fonseca. All rights reserved. | |
// | |
import Foundation | |
func JKLinearInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return t * end + (1.0 - t) * start; | |
} | |
//MARK: - Quadratic | |
func JKQuadraticInInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(t*t, start:start, end:end); | |
} | |
func JKQuadraticOutInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(2*t - t*t, start:start, end:end); | |
} | |
func JKQuadraticInOutInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
let middle = (start + end) / 2; | |
var tt = 2 * t; | |
if (tt <= 1) { | |
return JKLinearInterpolation(tt * tt, start:start, end:middle); | |
} | |
tt -= 1; | |
return JKLinearInterpolation(2*tt - tt * tt, start:middle, end:end); | |
} | |
//MARK: - Cubic | |
func JKCubicInInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(t*t*t, start:start, end:end); | |
} | |
func JKCubicOutInterpolation(t:Float, start:Float, end:Float) -> Float { | |
let tt = t-1; | |
return JKLinearInterpolation(tt*tt*tt + 1, start:start, end:end); | |
} | |
func JKCubicInOutInterpolation(t:Float, start:Float, end:Float) -> Float { | |
let middle = (start + end) / 2; | |
var tt = 2 * t; | |
if (tt <= 1) { | |
return JKLinearInterpolation(tt*tt*tt, start:start, end:middle); | |
} | |
tt -= 2; | |
return JKLinearInterpolation(tt*tt*tt + 1, start:middle, end:end); | |
} | |
//MARK: - Exponential | |
func JKExponentialInInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(powf(2, 10 * (t-1)), start:start, end:end); | |
} | |
func JKExponentialOutInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(-powf(2, -10 * t) + 1, start:start, end:end); | |
} | |
func JKExponentialInOutInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
let middle = (start + end) / 2.0; | |
var tt = t * 2.0; | |
if (tt < 1.0) { | |
return JKLinearInterpolation(powf(2, 10 * (tt-1)), start:start, end:middle); | |
} | |
tt--; | |
return JKLinearInterpolation(-powf(2, -10 * tt) + 1, start:middle, end:end); | |
} | |
//MARK: - Sinusoidal | |
func JKSinusoidalInInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(-cosf(t * Float(M_PI_2)) + 1, start:start, end:end); | |
} | |
func JKSinusoidalOutInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(sinf(t * Float(M_PI_2)), start:start, end:end); | |
} | |
func JKSinusoidalInOutInterpolation(t:Float, start:Float, end:Float) -> Float | |
{ | |
return JKLinearInterpolation(-0.5 * (cosf(t * Float(M_PI)) - 1), start:start, end:end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Straight port to Swift. I did change one important thing: I'm using Swift's Floats instead of CGFloat, making this pretty straight forward. But you'll probably need to do something like
CGFloat(JKLinearInterpolation(t, start:Float(start), end:Float(end))