二维arrays插值
我目前正在使用c#进行3D游戏。 我有一个名为data
二维数组,其中我获得了x
和y
值的z
值。 例如:
data[x,y] = z; data[1,2] = 4; data[2,4] = 5;
我的问题是这是非常模糊的,我还需要计算(插值)值,例如x = 1.5和y = 2.5。 如何获得此值并且是否有可用的function?
谢谢
也许双线性插值可以在你的场景中使用:
float fractionX = ... //the fraction part of the x coordinate float integerX = ... //the integer part of the x coordinate float fractionY, integerY = ... interpolatedValue = (1 - fractionX) * ((1 - fractionY) * data[integerX, integerY] + fractionY * data[integerX, integerY + 1]) + fractionX * ((1 - fractionY) * data[integerX + 1, integerY] + fractionY * data[integerX + 1, integerY + 1]);
在0,4,1和3之间插值产生以下结果:
如果您对高度图进行了三角测量,则重心插值可能更合适:
//Assuming the following triangle alignment: // 1 +--+--+--+ // | /| /| /| // |/ |/ |/ | // 0 +--+--+--+ if (fractionX < fractionY) //the upper triangle { interpolatedValue = (1 - fractionY) * data[integerX, integerY] + fractionX * data[integerX + 1, integerY + 1] + (fractionY - fractionX) * data[integerX, integerY + 1]; } else //the lower triangle { interpolatedValue = (1 - fractionX) * data[integerX, integerY] + fractionY * data[integerX + 1, integerY + 1] + (fractionX - fractionY) * data[integerX + 1, integerY]; }
在0,4,1和3之间插值产生以下结果:
你有两个已知点:
A = (1,2) = 4 B = (2,4) = 5
而你想要计算价值
C = (1.5, 2.5) = ???
以下是您的线性示例的概念。 计算每个轴的线性。 所以从X开始:
Ax = (1) = 4 Bx = (2) = 5 so you calculate Cx as: Cx = (1.5) = 4.5
然后计算y轴的线性:
Ay = (2) = 4 By = (4) = 5 and calculate Cy as: Cy = (2.5) = 4.25
然后平均Cx和Cy得到C(x,y)
C(1.5, 2.5) = (Cx + Cy) * 0.5 = 4.375