a = { 'x1': 0,
'y1': 0,
'x2': 10,
'y2': 5 }
Once we have written our tests, we can run the code. At this point they will fail as we have not written the code yet.
#!/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
False
>>>
>>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
>>> b = {'x1': 10, 'y1': 10, 'x2': 20, 'y2': 20}
>>> doRectanglesOverlap(a, b) # one corner touching
False
>>>
"""
return('BUG')
if __name__ == "__main__":
import doctest
doctest.testmod()
[Download]
#!/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
False
>>>
>>> a = {'x1': 0, 'y1': 0, 'x2': 10, 'y2': 10}
>>> b = {'x1': 10, 'y1': 10, 'x2': 20, 'y2': 20}
>>> doRectanglesOverlap(a, b) # one corner touching
False
>>>
"""
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 that the subroutine is written you can run the tests again. If any tests fail you can alter the code so that the tests pass.