Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class Rect:
  2.  
  3. def __init__(self, p1, p2):
  4. self.x1 = p1[0]
  5. self.y1 = p1[1]
  6. self.x2 = p2[0]
  7. self.y2 = p2[1]
  8.  
  9. def __repr__(self):# retezec
  10. return f"Rect(({self.x1},{self.y1}),({self.x2},{self.y2}))"
  11.  
  12. def perimeter(self):
  13. return 2 * (self.x2 - self.x1 + self.y2 - self.y1)
  14.  
  15. def area(self):
  16. return (self.x2 - self.x1) * (self.y2 - self.y1)
  17.  
  18. def __eq__(self, other):# boolean
  19. return (self.x1 == other.x1 and self.x2 == other.x2) and (self.y1 == other.y1 and self.y2 == other.y2)
  20.  
  21. def __contains__(self, other):# boolean
  22. return (self.x1 <= other.x1) and (self.x2 >= other.x2) and (self.y1 <= other.y1) and (self.y2 >= other.y2)
  23.  
  24. def __and__(self, other):
  25. x1 = max(self.x1, other.x1)
  26. y1 = max(self.y1, other.y1)
  27. x2 = min(self.x2, other.x2)
  28. y2 = min(self.y2, other.y2)
  29.  
  30. if (x1 < x2) and (y1 < y2):
  31. return Rect((x1, y1), (x2, y2))
  32. else:
  33. return Rect((self.x1, self.y1), (self.x1, self.y1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement