Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Rect:
- def __init__(self, p1, p2):
- self.x1 = p1[0]
- self.y1 = p1[1]
- self.x2 = p2[0]
- self.y2 = p2[1]
- def __repr__(self):# retezec
- return f"Rect(({self.x1},{self.y1}),({self.x2},{self.y2}))"
- def perimeter(self):
- return 2 * (self.x2 - self.x1 + self.y2 - self.y1)
- def area(self):
- return (self.x2 - self.x1) * (self.y2 - self.y1)
- def __eq__(self, other):# boolean
- return (self.x1 == other.x1 and self.x2 == other.x2) and (self.y1 == other.y1 and self.y2 == other.y2)
- def __contains__(self, other):# boolean
- return (self.x1 <= other.x1) and (self.x2 >= other.x2) and (self.y1 <= other.y1) and (self.y2 >= other.y2)
- def __and__(self, other):
- x1 = max(self.x1, other.x1)
- y1 = max(self.y1, other.y1)
- x2 = min(self.x2, other.x2)
- y2 = min(self.y2, other.y2)
- if (x1 < x2) and (y1 < y2):
- return Rect((x1, y1), (x2, y2))
- else:
- return Rect((self.x1, self.y1), (self.x1, self.y1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement