def constrain(pos, size):
    """constrain a position (pos) in the area defined by size"""
    x, y = pos
    w, h = size
    if x >= w: x = w-1
    if y >= h: y = h-1
    if x < 0: x = 0
    if y < 0: y = 0
    return x, y