pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Add invlerp() and remap() functions to pygame.math

Open bigwhoopgames opened this issue 1 year ago • 0 comments
trafficstars

Now that the lerp function has been implemented into pygame.math I thought it would also be a good idea to add the invlerp and remap functions.

def inv_lerp(a: float, b: float, v: float) -> float:
    return (v - a) / (b - a)

def remap(i_min: float, i_max: float, o_min: float, o_max: float, v: float) -> float:
    t = inv_lerp(i_min, i_max, v)
    return lerp(o_min, o_max, t)

inv_lerp is opposite of lerp and remap utilizes both to map a value from an input range to an output range. Very useful if you want to remap things like colors or percents etc.

bigwhoopgames avatar Jan 03 '24 15:01 bigwhoopgames