# linear.py : platform-independent linear transforms
# No copyright, 2020-2021, Garth Zeglin.  This file is explicitly placed in the public domain.

def map(x, in_min, in_max, out_min, out_max):
    """Map an input x from range (in_min, in_max) to range (out_min, out_max).  This
    is a Python implementation of the Arduino map() function.  Works on either
    integers or floating point.
    """
    divisor = in_max - in_min
    
    if divisor == 0:
        return out_min
    else:
        return ((x - in_min) * (out_max - out_min) / divisor) + out_min

def constrain(x, a, b):
    """Constrains a number x to be within range (a, b).  Python implementation
    of the Arduino constrain() function."""
    return min(max(x, a), b)

