c# - 将一个范围内的数字转换为另一个范围内的数字

标签 c# math scaling

<分区>

Possible Duplicate:
Convert a number range to another range, maintaining ratio

所以我有一个返回 0 到 255 之间的值的函数,我需要将这些值转换为 -255 到 255 之间的值 所以 200 大约是 145,150 大约是 45,依此类推。 我看过Convert a number range to another range, maintaining ratio但是那里的公式不起作用。 我可以使用任何其他公式吗?

最佳答案

public static int ConvertRange(
    int originalStart, int originalEnd, // original range
    int newStart, int newEnd, // desired range
    int value) // value to convert
{
    double scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
    return (int)(newStart + ((value - originalStart) * scale));
}

关于c# - 将一个范围内的数字转换为另一个范围内的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4229662/

相关文章:

c - 如何放大 mandelbrot 或 julia 集中的光标位置?

android - 可以安全地假设在横向模式下,高度总是小于宽度吗?

c# - 应用 'bouncy' 物理 Material 后,我无法让我的玩家统一跳跃相同的高度

c# - 从 Expression<Func<TModel, TProperty>> 转换为 Expression<Func<TModel, bool>>

c# - 如何合并两个排序功能?

c# - 为移动设备 (.NET) 在页面中排序框架( block )

database - 具有单一哈希函数的 LogLog 算法如何工作

java - 凸多面体的质心

java - 使用 ShapeRenderer 缩放 libGDX 中渲染的形状

python - NaN 设置为 -1 时缩放功能的影响