作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 C# 开发 3D 游戏。我有一个名为 data
的二维数组,其中我的 x
和 y
值得到一个 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 之间进行插值会产生以下结果:
如果您对高度图进行了三角测量,重心插值可能更合适:
//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 之间进行插值会产生以下结果:
关于c# - 二维数组插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22151994/
我是一名优秀的程序员,十分优秀!