

def clamp(x, a, b):
    m0 = min((a, b))
    m1 = max((a, b))
    return m0 if x < m0 else (m1 if x > m1 else x)

lerp = lambda x, xi, xf, ai, af: ai + ((x - xi) * (af - ai) / (xf - xi))
clerp = lambda x, xi, xf, ai, af: clamp(lerp(x, xi, xf, ai, af), ai, af)
