Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/usr/bin/env python
- class Point(object):
- def __init__(self, x, y):
- self.x = x
- self.y = y
- class Rect(object):
- def __init__(self, p1, p2):
- '''
- Store the top, bottom, left and right values for points
- p1 and p2 are the (corners) in either order
- '''
- self.left = min(p1.x, p2.x)
- self.right = max(p1.x, p2.x)
- self.bottom = min(p1.y, p2.y)
- self.top = max(p1.y, p2.y)
- def overlap(r1,r2):
- '''
- Overlapping rectangles overlap both horizontally & vertically
- '''
- hoverlaps = True
- voverlaps = True
- if (r1.left > r2.right) or (r1.right < r2.left):
- hoverlaps = False
- if (r1.top < r2.bottom) or (r1.bottom > r2.top):
- voverlaps = False
- return hoverlaps and voverlaps
- # --- main ---
- rect1 = Rect(Point(0, 0), Point(15, 15))
- rect2 = Rect(Point(10, 10), Point(20, 20))
- result = overlap(rect1, rect2)
- print(result)
- rect1 = Rect(Point(0, 0), Point(10, 10))
- rect2 = Rect(Point(15, 15), Point(20, 20))
- result = overlap(rect1, rect2)
- print(result)
- rect1 = Rect(Point(5, 0), Point(10, 15))
- rect2 = Rect(Point(0, 5), Point(15, 10))
- result = overlap(rect1, rect2)
- print(result)
- rect1 = Rect(Point(0, 0), Point(10, 10))
- rect2 = Rect(Point(15, 0), Point(20, 10))
- result = overlap(rect1, rect2)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement