Last active
November 2, 2015 01:23
-
-
Save philiptzou/4c6e41fa300964874009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from itertools import product | |
class Rect(object): | |
def __init__(self, x0, y0, x1, y1): | |
self.x = sorted((x0, x1)) | |
self.y = sorted((y0, y1)) | |
def __contains__(self, point): | |
x, y = point | |
return (self.x[0] <= x <= self.x[1] and | |
self.y[0] <= y <= self.y[1]) | |
def intersection(self, another): | |
for x0, y0, x1, y1 in product(self.x, self.y, | |
another.x, another.y): | |
point_a, point_b = (x0, y0), (x1, y1) | |
if point_a != point_b and \ | |
point_a in self and point_b in self and \ | |
point_a in another and point_b in another: | |
return Rect(x0, y0, x1, y1) | |
def __repr__(self): | |
return 'Rect({x[0]}, {y[0]}, {x[1]}, {y[1]})'.format( | |
x=self.x, y=self.y) | |
def overlap(rect_a, rect_b): | |
return bool(Rect(*rect_a).intersection(Rect(*rect_b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment