gpt4 book ai didi

c# - 二维数组插值

转载 作者:行者123 更新时间:2023-11-30 13:31:22 30 4
gpt4 key购买 nike

我目前正在使用 C# 开发 3D 游戏。我有一个名为 data 的二维数组,其中我的 xy 值得到一个 z 值。例如:

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

等我的问题是这非常模糊,我还需要计算(内插)值,例如 x=1.5 和 y=2.5。我怎样才能得到这个值,有没有可用的函数?

谢谢

最佳答案

也许双线性插值可以用在你的场景中:

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 之间进行插值会产生以下结果:

Bilinear 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

关于c# - 二维数组插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22151994/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com