Cubic/Curve Smooth Interpolation in C# [closed]

淺唱寂寞╮ 提交于 2019-12-20 19:37:52

问题


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.


回答1:


What you want is a Cubic Hermite Spline:

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




回答2:


you could have a linear interpolation and a cubic interpolation and interpolate between the two interpolation functions.

ie.

cubic(t) = cubic interpolation
linear(t) = linear interpolation
cubic_to_linear(t) = linear(t)*t + cubic(t)*(1-t)
linear_to_cubic(t) = cubic(t)*t + linear(t)*(1-t)

where t ranges from 0...1




回答3:


Well, a simple way would be this:

-Expand your function by 2 x and y
-Move 1 to the left and 1 down
Example: f(x) = -2x³+3x²
g(x) = 2 * [-2((x-1)/2)³+3((x-1)/2)²] - 1

Or programmatically (cubical adjusting):

double amountsub1div2 = (amount + 1) / 2;
amount = -4 * amountsub1div2 * amountsub1div2 * amountsub1div2 + 6 * amountsub1div2 * amountsub1div2 - 1;

For the other one, simply leave out the "moving":

g(x) = 2 * [-2(x/2)³+3(x/2)²]

Or programmatically (cubical adjusting):

double amountdiv2 = amount / 2;
amount = -4 * amountdiv2 * amountdiv2 * amountdiv2 + 6 * amountdiv2 * amountdiv2;


来源:https://stackoverflow.com/questions/1146281/cubic-curve-smooth-interpolation-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!