Skip to content

Instantly share code, notes, and snippets.

@carlosefonseca
Created November 8, 2015 13:53
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 carlosefonseca/6299b58cc7166a333c6c to your computer and use it in GitHub Desktop.
Save carlosefonseca/6299b58cc7166a333c6c to your computer and use it in GitHub Desktop.
//
// 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);
}
@carlosefonseca
Copy link
Author

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment