C#中的三次/曲线平滑插值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1146281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cubic/Curve Smooth Interpolation in C#
提问by Rob
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:
此函数将在给定 0.0f - 1.0f 之间的量的情况下在开始值和结束值之间进行三次插值。如果你要绘制这条曲线,你最终会得到这样的结果:
Expired Imageshack image removed
过期的 Imageshack 图像已删除
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
过期的 Imageshack 图像已删除
As one function
作为一项功能
and like this as another: (Cubic start to linear end)
并且像另一个这样:(三次开始到线性结束)
Expired Imageshack image removed
过期的 Imageshack 图像已删除
Anyone got any ideas? Thanks in advance.
有人有任何想法吗?提前致谢。
采纳答案by Donnie DeBoer
What you want is a Cubic Hermite Spline:
你想要的是一个三次 Hermite 样条:
where p0 is the start point, p1 is the end point, m0 is the start tangent, and m1 is the end tangent
其中 p0 是起点,p1 是终点,m0 是起点切线,m1 是终点切线
回答by Donnie DeBoer
you could have a linear interpolation and a cubic interpolation and interpolate between the two interpolation functions.
您可以进行线性插值和三次插值,并在两个插值函数之间进行插值。
ie.
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
其中 t 的范围为 0...1
回答by Jan Weber
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) = -2x3+3x2
g(x) = 2 * [-2((x-1)/2)3+3((x-1)/2)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+3(x/2)2]
Or programmatically (cubical adjusting):
或以编程方式(三次调整):
double amountdiv2 = amount / 2;
amount = -4 * amountdiv2 * amountdiv2 * amountdiv2 + 6 * amountdiv2 * amountdiv2;