has*_*ag7 3 c# interpolation multidimensional-array
我目前正在使用c#进行3D游戏.我有一个二维数组data,我得到了z我x和y值的值.例如:
data[x,y] = z;
data[1,2] = 4;
data[2,4] = 5;
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是非常模糊的,我还需要计算(插值)值,例如x = 1.5和y = 2.5.如何获得此值并且是否有可用的功能?
谢谢
Nic*_*ler 10
也许双线性插值可以在你的场景中使用:
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]);
Run Code Online (Sandbox Code Playgroud)
在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];
}
Run Code Online (Sandbox Code Playgroud)
在0,4,1和3之间插值产生以下结果:
