Cubic/Curve Smooth Interpolation in C#

Rob picture Rob · Jul 18, 2009 · Viewed 12.1k times · Source

Below is a cubic interpolation function:

public float Smooth(float start, float end, float amount)
{
    // Clamp to 0-1;
    amount = (amount > 1f) ? 1f : amount;
    amount = (amount < 0f) ? 0f : amount;

    // Cubicly adjust the amount value.
    amount = (amount * amount) * (3f - (2f * amount));

    return (start + ((end - start) * amount));
}

This function will cubically interpolate between the start and end value given an amount between 0.0f - 1.0f. If you were to plot this curve, you'd end up with something like this:

Expired Imageshack image removed

The cubic function here is:

    amount = (amount * amount) * (3f - (2f * amount));

How do I adjust this to produce two produce tangents in and out?

To produce curves like this: (Linear start to cubic end)

Expired Imageshack image removed

As one function

and like this as another: (Cubic start to linear end)

Expired Imageshack image removed

Anyone got any ideas? Thanks in advance.

Answer

Donnie DeBoer picture Donnie DeBoer · Jul 18, 2009

What you want is a Cubic Hermite Spline:

alt text

where p0 is the start point, p1 is the end point, m0 is the start tangent, and m1 is the end tangent