C#中的立方/曲线平滑插值

时间:2009-07-18 00:29:53

标签: c# math interpolation linear bicubic

下面是三次插值函数:

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

此函数将在起始值和结束值之间进行立方插值,给定量在0.0f - 1.0f之间。如果你要绘制这条曲线,你最终会得到这样的结果:

  

已删除过期的Imageshack图像

这里的立方函数是:

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

如何调整此项以产生两个切线进出?

生成如下曲线:(线性开始到立方结束)

  

已删除过期的Imageshack图像

作为一个功能

并将此视为另一种:(立方开始线性结束)

  

已删除过期的Imageshack图像

有人有任何想法吗?提前谢谢。

3 个答案:

答案 0 :(得分:12)

你想要的是Cubic Hermite Spline

alt text

其中p0是起点,p1是终点,m0是起始切线,m1是结束切线

答案 1 :(得分:3)

你可以进行线性插值和三次插值,并在两个插值函数之间进行插值。

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)

其中t的范围是0 ... 1

答案 2 :(得分:0)

嗯,一个简单的方法就是:

-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

或以编程方式(立方体调整):

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

对于另一个,只需忽略&#34;移动&#34;:

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

或以编程方式(立方体调整):

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