Remapping a Range

Written by me (Anixias).

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


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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s