|
| 1 | +from itertools import product, tee |
| 2 | + |
| 3 | +def pairwise(iterable): |
| 4 | + "s -> (s0, s1), (s1, s2), (s2, s3), ..." |
| 5 | + a, b = tee(iterable) |
| 6 | + next(b, None) |
| 7 | + return zip(a, b) |
| 8 | + |
| 9 | +class Rectangle: |
| 10 | + |
| 11 | + __slots__ = '__x1', '__y1', '__x2', '__y2' |
| 12 | + |
| 13 | + def __init__(self, x1, y1, x2, y2): |
| 14 | + self.__setstate__((min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))) |
| 15 | + |
| 16 | + def __repr__(self): |
| 17 | + return '{}({})'.format(type(self).__name__, ', '.join(map(repr, self))) |
| 18 | + |
| 19 | + def __eq__(self, other): |
| 20 | + return self.data == other.data |
| 21 | + |
| 22 | + def __ne__(self, other): |
| 23 | + return self.data != other.data |
| 24 | + |
| 25 | + def __hash__(self): |
| 26 | + return hash(self.data) |
| 27 | + |
| 28 | + def __len__(self): |
| 29 | + return 4 |
| 30 | + |
| 31 | + def __getitem__(self, key): |
| 32 | + return self.data[key] |
| 33 | + |
| 34 | + def __iter__(self): |
| 35 | + return iter(self.data) |
| 36 | + |
| 37 | + def __and__(self, other): |
| 38 | + x1, y1, x2, y2 = max(self.x1, other.x1), max(self.y1, other.y1), \ |
| 39 | + min(self.x2, other.x2), min(self.y2, other.y2) |
| 40 | + if x1 < x2 and y1 < y2: |
| 41 | + return type(self)(x1, y1, x2, y2) |
| 42 | + |
| 43 | + def __sub__(self, other): |
| 44 | + intersection = self & other |
| 45 | + if intersection is None: |
| 46 | + yield self |
| 47 | + else: |
| 48 | + x, y = {self.x1, self.x2}, {self.y1, self.y2} |
| 49 | + if self.x1 < other.x1 < self.x2: |
| 50 | + x.add(other.x1) |
| 51 | + if self.y1 < other.y1 < self.y2: |
| 52 | + y.add(other.y1) |
| 53 | + if self.x1 < other.x2 < self.x2: |
| 54 | + x.add(other.x2) |
| 55 | + if self.y1 < other.y2 < self.y2: |
| 56 | + y.add(other.y2) |
| 57 | + for (x1, x2), (y1, y2) in product(pairwise(sorted(x)), |
| 58 | + pairwise(sorted(y))): |
| 59 | + instance = type(self)(x1, y1, x2, y2) |
| 60 | + if instance != intersection: |
| 61 | + yield instance |
| 62 | + |
| 63 | + def __getstate__(self): |
| 64 | + return self.x1, self.y1, self.x2, self.y2 |
| 65 | + |
| 66 | + def __setstate__(self, state): |
| 67 | + self.__x1, self.__y1, self.__x2, self.__y2 = state |
| 68 | + |
| 69 | + @property |
| 70 | + def x1(self): |
| 71 | + return self.__x1 |
| 72 | + |
| 73 | + @property |
| 74 | + def y1(self): |
| 75 | + return self.__y1 |
| 76 | + |
| 77 | + @property |
| 78 | + def x2(self): |
| 79 | + return self.__x2 |
| 80 | + |
| 81 | + @property |
| 82 | + def y2(self): |
| 83 | + return self.__y2 |
| 84 | + |
| 85 | + @property |
| 86 | + def width(self): |
| 87 | + return self.x2 - self.x1 |
| 88 | + |
| 89 | + @property |
| 90 | + def height(self): |
| 91 | + return self.y2 - self.y1 |
| 92 | + |
| 93 | + @property |
| 94 | + def area(self): |
| 95 | + return self.width * self.height |
| 96 | + |
| 97 | + def iou(self, other): |
| 98 | + inter = self & other |
| 99 | + return inter.area / (self.area + other.area - inter.area) |
| 100 | + |
| 101 | + intersection = __and__ |
| 102 | + |
| 103 | + difference = __sub__ |
| 104 | + |
| 105 | + data = property(__getstate__) |
| 106 | + |
0 commit comments