Skip to content

Instantly share code, notes, and snippets.

@laundmo
Last active September 5, 2024 10:38
Show Gist options
  • Save laundmo/b224b1f4c8ef6ca5fe47e132c8deab56 to your computer and use it in GitHub Desktop.
Save laundmo/b224b1f4c8ef6ca5fe47e132c8deab56 to your computer and use it in GitHub Desktop.
lerp, inverse lerp and remap in python
def lerp(a: float, b: float, t: float) -> float:
"""Linear interpolate on the scale given by a to b, using t as the point on that scale.
Examples
--------
50 == lerp(0, 100, 0.5)
4.2 == lerp(1, 5, 0.8)
"""
return (1 - t) * a + t * b
def inv_lerp(a: float, b: float, v: float) -> float:
"""Inverse Linar Interpolation, get the fraction between a and b on which v resides.
Examples
--------
0.5 == inv_lerp(0, 100, 50)
0.8 == inv_lerp(1, 5, 4.2)
"""
return (v - a) / (b - a)
def remap(i_min: float, i_max: float, o_min: float, o_max: float, v: float) -> float:
"""Remap values from one linear scale to another, a combination of lerp and inv_lerp.
i_min and i_max are the scale on which the original value resides,
o_min and o_max are the scale to which it should be mapped.
Examples
--------
45 == remap(0, 100, 40, 50, 50)
6.2 == remap(1, 5, 3, 7, 4.2)
"""
return lerp(o_min, o_max, inv_lerp(i_min, i_max, v))
@companje
Copy link

thanks!

@SuzukaDev
Copy link

Very handy, thank you so much!

@Darcvizer
Copy link

+1
Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment