GML中的多变量插值误差

时间:2013-08-29 07:14:08

标签: linear-interpolation

大家。我正在使用Game Maker,一个语法与Python类似的程序(除了间距)来实现多个数据值之间的插值,如here所述。想象一个散点图,其原点x = 0,末尾x = 100,每个数据值之间的间距相等。 x位置是恒定的,但是y位置可以具有任何值。如果在每个数据点之间连接了线,那么理想情况下,脚本将能够找到给定x位置的y。这是我最初的实现:

// This is executed once.

nums = 3; // the number of values to interpolate between
t = 0; // the position along the "scatterplot" of values

// Values may be decimals with any sign.  There should be values >= nums.
ind[0] = 100;
ind[1] = 0;
ind[2] = 100;

//This is executed each step.

intervals = 100 / (nums - 1);
index = round(percent / intervals);
if percent != 0 {newpercent=  (intervals / (percent - index)) / 100;}
else {newpercent = 0;}
newval = ind[index] * (1 - newpercent) + ind[index + 1] * newpercent;

这应该在找到哪两个点围绕给定的x位置之后使用lerp()来返回它们的两个值之间的插值,但它没有,所以我的问题是:
出了什么问题,如何解决?提前致谢。

编辑:这是完成且正常工作的代码:

// This is executed once.

nums = 3; // the number of values to interpolate between
t = 0; // the position along the "scatterplot" of values

// Values may be decimals with any sign.  There should be values >= nums.
ind[0] = 100;
ind[1] = 0;
ind[2] = 100;

// This is executed each step; setting the result to 'alpha'.

if (nums > 1) {
    if (t != 1) {
        _temp1 = 1 / (nums - 1);
        _temp2 = floor(t / _temp1);
        _temp3 = (t - (_temp1 * _temp2)) * (nums - 1);
        alpha = ind[_temp2] + _temp3 * (ind[_temp2 + 1] - ind[_temp2]);
    }
    else {
        alpha = ind[nums - 1];
    }
}

1 个答案:

答案 0 :(得分:0)

你要做的是插入游戏属性的值(音量,危险等级,重力等)让我们调用你想要计算y的变量,因为其他一些属性会发生变化(比如时间, x位置等)让我们称之为t

我们知道n的价值y点。让我们调用这些点p0p1 ... pn-1中的每一个,其中每个数字都是该点的index。让我们调用这些点中的值y0y1 ... yn-1。 因此,对于t的任何给定值,我们希望执行以下操作:

首先我们找到最接近t的两个点。由于所有点均匀分布,我们知道给定点的t值为t = index/(n-1),并且通过重新排序此等式,我们可以得到任何给定t的“索引”,如{{1} }。当index = t*(n-1)与我们的某个点之间的位置不完全相同时,它将成为两个最接近的点tpk的索引值之间的数字。因此,pk1会为您提供pk = floor(index)之前的索引,t是下一个点。

然后我们必须找出这两个点(0到1之间的值)的接近pk1 = pk + 1是多少,因为这决定了每个点的值在插值中的影响程度。我们称这个衡量为t。然后是alpha

最后,如果alpha = (t - pk)/(pk1 - pk)pk的值为pk1yk,则会得到这样的插值[{1}}

yk1
相关问题