def square_rect_index(x, y, rect_x, rect_y, width, height):
    """Given a square and a rectangle, gives a linear position of the square.
    The index is an integer between zero and square_rect_size - 1.
    This is useful for array storage of rectangles.
    Returns None if the square is not in the rectangle.
    Equivalent to list(square_rect(...)).index((x, y))"""
    dx = x - rect_x
    dy = y - rect_y
    if dx < 0 or dx >= width or dy < 0 or dy >= height:
        return None
    return dx + dy * width