Skip to content

Instantly share code, notes, and snippets.

@thobroni
Created May 21, 2017 03:34
Show Gist options
  • Save thobroni/2f3ab90e7f8b79bbfc3981a6cf573bf3 to your computer and use it in GitHub Desktop.
Save thobroni/2f3ab90e7f8b79bbfc3981a6cf573bf3 to your computer and use it in GitHub Desktop.
Python number mapping, taken from : http://stackoverflow.com/a/1969274
def mapping(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment