Advertisement
1nikitas

Untitled

Mar 20th, 2022
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. class Rectangle:
  2.     def __init__(self, x, y, w, h):
  3.         self.x = x
  4.         self.y = y
  5.         self.w = w
  6.         self.h = h
  7.  
  8.     def intersection(self, other):
  9.         if ((other.x >= self.x + self.w) or (other.y >= self.y + self.h) or
  10.                 (self.x >= other.x + other.w) or (self.y >= other.y + other.h)):
  11.             return None
  12.         else:
  13.             R3 = Rectangle(0, 0, 0, 0)
  14.             R3.x = max(self.x, other.x)
  15.             R3.w = min(self.x + self.w, other.x + other.w) - R3.x
  16.             R3.y = max(self.y, other.y)
  17.             R3.h = min(self.y + self.h, other.y + other.h) - R3.y
  18.             return R3
  19.  
  20.     def get_x(self):
  21.         return self.x
  22.    
  23.     def get_y(self):
  24.         return self.y
  25.    
  26.     def get_w(self):
  27.         return self.w
  28.    
  29.     def get_h(self):
  30.         return self.h
  31.  
  32.  
  33. if __name__ == '__main__':
  34.     rect1 = Rectangle(0.1, 0.2, 10.8, 10.07)
  35.     rect2 = Rectangle(5.4, 5.8, 10.25, 10.45)
  36.     if rect1.intersection(rect2):
  37.         rect3 = rect1.intersection(rect2)
  38.         print((rect3.x, rect3.y, rect3.w, rect3.h))
  39.         print(rect3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement