Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Created March 19, 2015 16:37
Show Gist options
  • Save krhoyt/fa2a1dc71eab10d5a671 to your computer and use it in GitHub Desktop.
Save krhoyt/fa2a1dc71eab10d5a671 to your computer and use it in GitHub Desktop.
Linear transform to map a value in one range to a value in another.
function scale( value, old_top, old_bottom, new_top, new_bottom )
{
// minTo + (maxTo - minTo) * ((value - minFrom) / (maxFrom - minFrom))
return new_bottom + ( new_top - new_bottom ) * ( ( value - old_bottom ) / ( old_top - old_bottom ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment