-2

我必须实现小的多图像图形控件,它本质上是一个由 9 个图像组成的数组,一个一个地显示。最终目标是充当迷你滑块。

现在,这个图形控件将接收各种整数范围:从 5 到 25 或从 0 到 7 或从 -9 到 9。

如果我要使用比例——“三法则”恐怕在技术上是不可行的,因为它可能是错误的来源。我的猜测是使用一些查找表,但是有没有人对方法有好的建议?

谢谢

4

2 回答 2

1

我不确定是否需要查找表。您可以从输入值按比例获取 0 到 9 之间的图像索引:

int ConvertToImageArrayIndex(int inputValue)
{
    int maxInputFromOtherModule = 25;
    int minInputFromOtherModule = 5;


    // +1 required so include both min and max input values in possible range.
    // + 0.5 required so that round to the nearest image instead of always rounding down.
    // 8.0 required to get to an output range of 9 possible indexes [0..8]

    int imageIndex = ( (float)((inputValue-minInputFromOtherModule) * 8.0) / (float)(maxInputFromOtherModule - minInputFromOtherModule + 1) ) + 0.5;

    return imageIndex;
}
于 2010-08-04T08:43:50.483 回答
0

是的,查找表是一个很好的解决方案

int lookup[9] = {5, 25, ... the other values };
int id1 = floor(slider);
int id2 = id1+1;
int texId1 = lookup[id1];
int texId2 = lookup[id2];
interpolate(texId1, texId2, slider - float(id1));
于 2010-08-04T08:11:59.503 回答