Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

obj-c linear interpolation between two numbers

Just wondering if there are methods already implemented for handling linear interpolation between two numbers in foundation/something else that comes with Xcode? It's hardly an advanced thing to implement yourself, but I usually find myself reimplementing things that have already been implemented, and it's nice to use functionality that already exists (plus it's more standardized).

So what I'd like is something like this:

lerp(number1, number2, numberBetween0And1);

// Example:
lerp(0.0, 10.0, .5); // returns 5.0

Does it exist?

like image 425
quano Avatar asked Nov 30 '09 16:11

quano


1 Answers

No, but it's an easy one-liner:

inline double lerp(double a, double b, double t)
{
    return a + (b - a) * t;
}

inline float lerpf(float a, float b, float t)
{
    return a + (b - a) * t;
}
like image 165
Adam Rosenfield Avatar answered Sep 22 '22 00:09

Adam Rosenfield