2D Array Interpolation

一笑奈何 提交于 2019-12-01 09:50:25

Maybe Bilinear Interpolation can be used in your scenario:

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]);

Interpolating between 0, 4, 1 and 3 yields the following result:

If you have triangulated the height map, Barycentric Interpolation might be more appropriate:

//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];
}

Interpolating between 0, 4, 1 and 3 yields the following result:

You have two known points:

A = (1,2) = 4
B = (2,4) = 5

And you want to calculate the value

C = (1.5, 2.5) = ???

Here's an idea that follows from your linear example. Calculate the linear for each axis. So start with X:

Ax = (1) = 4
Bx = (2) = 5
so you calculate Cx as:
Cx = (1.5) = 4.5

Then calculate the linear for the y-axis:

Ay = (2) = 4
By = (4) = 5
and calculate Cy as:
Cy = (2.5) = 4.25

Then average Cx and Cy to get C(x,y)

C(1.5, 2.5) = (Cx + Cy) * 0.5 = 4.375
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!