i am currently working on a 3D game in c#. I have a 2 dimensional array called data
where i get a z
value for my x
and y
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: