To map any range [in_min, in_max]
to [out_min, out_max]
we can use this function:
float map_range(float s, float in_min, float in_max, float out_min, float out_max)
{
assert(in_min <= s && s <= in_max);
return out_min + (s - in_min) * (out_max - out_min) / (in_max - in_min);
}
There are a few special cases where we can simplify this:
[0, 1]
to [out_min, out_max]
:float map_range(float s, float out_min, float out_max)
{
return out_min + s * (out_max - out_min);
}
[-1, 1]
to [0, 1]
:float map_range(float s)
{
assert(-1.0f <= s && s <= 1.0f);
return (s + 1.0f) / 2.0f;
}
[-0.5, 0.5]
to [0, 1]
:float map_range(float s)
{
assert(-0.5f <= s && s <= 0.5f);
return s + 0.5f;
}
[0, 1]
to [-1, 1]
:float map_range(float s)
{
assert(0.0f <= s && s <= 1.0f);
return s * 2.0f - 1.0f;
}