Boxes

We represent a two-dimensional point in Python with a tuple. It has two components which reperesent the x and y coordinates (real or integer). We also represent a two-dimensional orthogonal rectangle with a tuple. It has two components which reperesent its bottom left and top right vertices, respectively, as points.

Save the following functions into a file named rectangles.py.

  1. Write the function point_inside_rect() that takes a two-dimensional point and an orthogonal rectangle, and returns True if the point in inside the rectangle and False otherwise. Points on the rectangle boundary are considered interior.

    See the following examples:

    >>> point_inside_rect ((2, 2), ((1, 1), (4, 8)))
    True
    >>> point_inside_rect ((2, 2), ((1, 3), (4, 8)))
    False
    >>> point_inside_rect ((1, 8), ((1, 3), (4, 8)))
    True
    
    

    Note

    More tests are provided in the test-point_inside_rect.txt file.

  2. Write the function rect_number() that takes a two-dimensional point and a list of orthogonal rectangles, and returns the number of rectangles that contain this point. This function must call the first one.

    See the following examples:

    >>> rect_number ((2, 2), lrect)
    2
    >>> rect_number ((-2, -2), lrect)
    0
    >>> rect_number ((0, 1), lrect)
    1
    

    Note

    More tests are provided in the test-rect_number.txt file.

  3. Write the function rectangle_cont() that takes a two-dimensional point and a list of orthogonal rectangles, and returns the first rectangle in the list which contains the given point. If there is no such rectangle, the function must return a rectangle with both points equal to the given one. This function must call the first one.

    See the following examples:

    >>> rectangle_cont ((2, 2), lrect)
    ((1, 1), (4, 8))
    >>> rectangle_cont ((5, 6), lrect)
    ((5, 6), (5, 6))
    

    Note

    More tests are provided in the test-rectangle_cont.txt file

  4. Write the function rectangles() that takes a two-dimensional point and a tuple of orthogonal rectangles, and returns another tuple with all the rectangles in the given tuple that contain the given point. The rectangles in the returned tuple must be in the same order as in the given one. This function must call the first one.

    See the following examples:

    >>> lrect = (((1, 1), (4, 8)), ((1, 3), (4, 8)), ((0, 0), (3, 4)))
    >>> rectangles ((2, 2), lrect)
    (((1, 1), (4, 8)), ((0, 0), (3, 4)))
    

    Note

    More tests are provided in the test-rectangles.txt file

Solution

A solution of these functions is provided in file rectangles.py,