Skip to content

Instantly share code, notes, and snippets.

@chadgh
Last active September 1, 2015 19:40
Show Gist options
  • Save chadgh/7be05558c4c07a69f308 to your computer and use it in GitHub Desktop.
Save chadgh/7be05558c4c07a69f308 to your computer and use it in GitHub Desktop.
Function to create an interpolate function.
def make_interpolation(from_range, to_range):
from_min, from_max = from_range
to_min, to_max = to_range
scale = float(to_max - to_min) / float(from_max - from_min)
def interpolate(value):
return to_min + (value - from_min) * scale
return interpolate
change = make_interpolation((0, 100), (0, 200))
print(change(50)) # will print 100.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment