obj-c linear interpolation between two numbers

2020-06-03 01:31发布

问题:

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?

回答1:

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;
}