def linear_map(x, start1, stop1, start2, stop2):
    """
    Linearly map x from [start1, stop1] to [start2, stop2]
    Equivalent to Processing's map function. If x is outside [star1, stop1], it
    will be extrapolated linearly.
    """
    scale = float((stop2 - start2)) / (stop1 - start1)
    return (x - start1) * scale + start2