Invert GMLscripts.com

map_range

Remaps a value from one range to another range.

Given two ranges \([a_1,a_2]\) and \([b_1,b_2]\), a value \(s\) in range \([a_1,a_2]\) is linearly mapped to a value \(t\) in range \([b_1,b_2]\), where: $$t = b_1+\frac{(s-a_1)(b_2-b_1)}{(a_2-a_1)}$$

// Map radians to degrees
degrees = map_range(n, -pi, pi, -180, 180);

// Map unit UV coordinates to texture page sprite
oldUV = [0.25, 0.33];
texUV = sprite_get_uvs(sprite, subimage);
newUV[0] = map_range(oldUV[0], 0.0, 1.0, texUV[0], texUV[2]);
newUV[1] = map_range(oldUV[1], 0.0, 1.0, texUV[1], texUV[3]);
map_range(x, a1, a2, b1, b2)
Returns a value remapped from one range to another.
COPY/// @func   map_range(x, a1, a2, b1, b2)
///
/// @desc   Returns a value remapped from one range to another.
///         Values outside the bounds of the current range will
///         be remapped outside the bounds of the new range.
///
/// @param  {real}      x           value to remap
/// @param  {real}      a1          start of current range
/// @param  {real}      a2          end of current range
/// @param  {real}      b1          start of new range
/// @param  {real}      b2          end of new range
///
/// @return {real}      remapped value
///
/// GMLscripts.com/license

function map_range(x, a1, a2, b1, b2)
{
    return b1 + (b2-b1) * (x-a1) / (a2-a1);
}

Contributors: xot

GitHub: View · Commits · Blame · Raw