Dealing With Change

Handling Changing Specifications

At some point after completing the code, the specification is changed so that touching rectangles do overlap.

changes
#!/usr/bin/env python3

def doRectanglesOverlap(a, b):
    """ Tests whether two rectangles overlap

    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 5}
    >>> b = {'x1': 15, 'y1': 0, 'x2': 25, 'y2': 5}
    >>> doRectanglesOverlap(a, b) # not overlapping, not touching
    False
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 5}
    >>> b = {'x1': 5, 'y1': 3, 'x2': 15, 'y2': 13}
    >>> doRectanglesOverlap(a, b) # one corner in the other rectangle
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 5}
    >>> b = {'x1': 5, 'y1': -3, 'x2': 15, 'y2': 8}
    >>> doRectanglesOverlap(a, b) # two corners in the other rectangle
    True
    >>> 
    >>> a = {'x1': 1, 'y1': 0, 'x2': 11, 'y2': 10}
    >>> b = {'x1': 0, 'y1': 1, 'x2': 12, 'y2': 9}
    >>> doRectanglesOverlap(a, b) # overlapping, neither rectangle contains the other's corner
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
    >>> b = {'x1': 1, 'y1': 1, 'x2': 9, 'y2': 9}
    >>> doRectanglesOverlap(a, b) # one rectangle containing the other
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
    >>> b = {'x1': 10, 'y1': 0, 'x2': 20, 'y2': 10}
    >>> doRectanglesOverlap(a, b) # one edge touching
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
    >>> b = {'x1': 10, 'y1': 10, 'x2': 20, 'y2': 20}
    >>> doRectanglesOverlap(a, b) # one corner touching
    True
    >>> 
    """
    
    if a['x1'] < b['x2'] and a['x2'] > b['x1'] and a['y1'] < b['y2'] and a['y2'] > b['y1']:
        return True

    return False
    


if __name__ == "__main__":
   import doctest
   doctest.testmod()


[Download]

Now rerun the tests. The tests you have changed should now fail. You can now change the subroutine code.

#!/usr/bin/env python3

def doRectanglesOverlap(a, b):
    """ Tests whether two rectangles overlap

    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 5}
    >>> b = {'x1': 15, 'y1': 0, 'x2': 25, 'y2': 5}
    >>> doRectanglesOverlap(a, b) # not overlapping, not touching
    False
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 5}
    >>> b = {'x1': 5, 'y1': 3, 'x2': 15, 'y2': 13}
    >>> doRectanglesOverlap(a, b) # one corner in the other rectangle
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 5}
    >>> b = {'x1': 5, 'y1': -3, 'x2': 15, 'y2': 8}
    >>> doRectanglesOverlap(a, b) # two corners in the other rectangle
    True
    >>> 
    >>> a = {'x1': 1, 'y1': 0, 'x2': 11, 'y2': 10}
    >>> b = {'x1': 0, 'y1': 1, 'x2': 12, 'y2': 9}
    >>> doRectanglesOverlap(a, b) # overlapping, neither rectangle contains the other's corner
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
    >>> b = {'x1': 1, 'y1': 1, 'x2': 9, 'y2': 9}
    >>> doRectanglesOverlap(a, b) # one rectangle containing the other
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
    >>> b = {'x1': 10, 'y1': 0, 'x2': 20, 'y2': 10}
    >>> doRectanglesOverlap(a, b) # one edge touching
    True
    >>> 
    >>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
    >>> b = {'x1': 10, 'y1': 10, 'x2': 20, 'y2': 20}
    >>> doRectanglesOverlap(a, b) # one corner touching
    True
    >>> 
    """
    
    if a['x1'] <= b['x2'] and a['x2'] >= b['x1'] and a['y1'] <= b['y2'] and a['y2'] >= b['y1']:
        return True

    return False
    


if __name__ == "__main__":
   import doctest
   doctest.testmod()


[Download]

The < and > signs have been changed to <= and >=.
Continue