C++ 中的查找表

发布于 2024-09-12 18:17:23 字数 200 浏览 8 评论 0原文

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

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

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

谢谢

I have to implement small multimage graphic control, which in essence is an array of 9 images, shown one by one. The final goal is to act as minislider.

Now, this graphic control is going to receive various integer ranges: from 5 to 25 or from 0 to 7 or from -9 to 9.

If I am going to use proportion - "rule of three" I am afraid is not technically suistainable because it can be a source of errors. My guess is to use some lookup tables, but has anyone an good advice for approach?

Thnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-09-19 18:17:27

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

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

yes, a lookup table is a good solution

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));
静待花开 2024-09-19 18:17:26

我不确定是否需要查找表。您可以按比例从输入值获取 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;
}

I'm not sure look up tables are required. You can get from your input value to an image index between 0 and 9 proportionally:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文