大家。我正在使用 Game Maker,这是一个语法有点类似于 Python(除了间距)的程序来实现多个数据值之间的插值,如此处所述。想象一个散点图,原点 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];
}
}