Remapping a Range

Written by me (Anixias).

function map_range(value, srcmin, srcmax, destmin, destmax)
{
	return destmin + (destmax - destmin) * ((value - srcmin) / (srcmax - srcmin));
}

Usage

You can use this function to map one range to another. For example, say you want to convert a point in the view to a point in the GUI. You could call something like (pseudo-code):

point_x_in_gui = map_range(point_x_in_view, view_x, view_x + view_width, 0, display_get_gui_width());
point_y_in_gui = map_range(point_y_in_view, view_y, view_y + view_height, 0, display_get_gui_height());

Leave a comment