C++ 'map' 数字范围

标签 c++ c

在 C 或 C++ 中,或者只是在一般数学上,是否有更好的方法来映射数字比率,同时保留或舍入数据?

Take the following example

double cdp = 17000.0;    
float integ = 30000.0 / 255;
int val = cdp / integ;

color = color + RGB(val, val, val);

这里我想将数字范围 [0, 30000] 映射到值 [0, 255]

最佳答案

如果我理解正确的话,您可以使用简单的线性插值来完成。它会像这样工作:

x = (inValue - minInRange) / (maxInRange - minInRange);
result = minOutRange + (maxOutRange - minOutRange) * x

在您的示例中,inValue 是 30,000 中的数字。 minInRange = 0,maxInRange = 30,000,minOutRange = 0,maxOutRange = 255。

关于C++ 'map' 数字范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8684327/

相关文章:

c - 在 Swift 中读取二进制 (.hgt) 文件(将代码从 C++ 迁移到 swift)

java - 如何将 char 数组从 C JNI 函数作为 byte[] 传递给 Java 方法

c - 读取中间值时双向 fifo 通信阻塞

C++ if 语句

c++ - 为什么有人会使用 set 而不是 unordered_set?

c++ - R:dyn.load 错误(文件,DLLpath = DLLpath,...)

c - while语句中的 sleep 函数

c - 为什么在 Linux 中内存使用量大于物理 RAM?

c++ - Cocos2dx、box2d坦克

c++ - 帮助在 C++ 中使用 NetuserAdd() 和 NetLocalGroupAddMembers()