Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import reduce
- from itertools import combinations as comb
- from dataclasses import dataclass, astuple
- @dataclass
- class Box:
- x1:float
- y1:float
- x2:float
- y2:float
- # зануление координат если он неправильный или пустой
- def __post_init__(self):
- if self.x1 >= self.x2 or self.y1 >= self.y2:
- self.x1 = self.y1 = self.x2 = self.y2 = 0
- # находит прямоугольник-пересечение двух прямоугольников
- def intersect(self, other):
- if astuple(self) == (0,0,0,0) or astuple(other) == (0,0,0,0):
- return Box(0,0,0,0)
- x1 = max(self.x1, other.x1)
- x2 = min(self.x2, other.x2)
- y1 = max(self.y1, other.y1)
- y2 = min(self.y2, other.y2)
- return Box(x1, y1, x2, y2)
- #площадь прямоугольника
- def S(self):
- return (self.y2-self.y1)*(self.x2-self.x1)
- def __str__(self):
- return f"{self.x1}, {self.y1} : {self.x2}, {self.y2}\n"
- A = Box(3, 3, 5, 5)
- B = Box(0, 0, 2, 5)
- C = Box(1, 2, 4, 4)
- D = Box(3, 1, 5, 4)
- E = Box(10, 1, 15, 2)
- lst = [D,B,C,A, E]
- res = 0;
- #по принципу включения-исключения из комбинаторики
- for i in range(len(lst)):
- res += (-1)**i * sum(reduce(Box.intersect, seq).S() for seq in comb(lst, i+1))
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment