2D array interpolation

I am currently working on a 3D game in C #. I have a 2 dimensional array named datawhere I get a value zfor my xand values y. eg:

data[x,y] = z;
data[1,2] = 4;
data[2,4] = 5;

etc .. my problem is that it is very vague and I also need calculated (interpolated) values, for example x = 1.5 and y = 2.5. How can I get this value and are any functions available?

thank

+1
source share
2 answers

Perhaps your scenario may use bilinear interpolation:

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

Interpolation between 0, 4, 1 and 3 gives the following result:

Bilinear interpolation

, Barycentric Interpolation :

//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 :

Barycentric interpolation

+10

:

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
+1

Source: https://habr.com/ru/post/1547837/


All Articles