Advertisement
SimeonTs

SUPyF Objects and Classes - 03. Rectangle Position

Aug 12th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. """
  2. Objects and Classes
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/950#2
  4.  
  5. SUPyF Objects and Classes - 03. Rectangle Position
  6.  
  7. Problem:
  8. Write a program to read two rectangles {left, top, width, height} and print whether the first is inside the second.
  9. The input is given as two lines, each holding a rectangle, described by 4 integers: left, top, width and height.
  10.  
  11. Examples:
  12.    Input:          |   Output:
  13.        4 -3 6 4    |       Inside
  14.        2 -3 10 6   |
  15. -------------------------------------
  16.    Input:          |   Output:
  17.        2 -3 10 6    |       Not inside
  18.        4 -5 6 10   |
  19.  
  20. Hints
  21.    - Create a class Rectangle holding properties Top, Left, Width and Height.
  22.    - Define calculated properties Right and Bottom.
  23.    - Define a method is_inside(rectangle). A rectangle r1 is inside another rectangle r2 when:
  24.        - r1.left ≥ r2.left
  25.        - r1.right ≤ r2.right
  26.        - r1.top ≤ r2.top
  27.        - r1.bottom ≤ r2.bottom
  28.    - Create a method to read a Rectangle.
  29.    - Combine all methods into a single program.
  30. """
  31.  
  32.  
  33. class Rect:
  34.     def __init__(self, left, right, width, height):
  35.         self.left = left
  36.         self.right = right
  37.         self.bottom = right + height
  38.         self.top = left + width
  39.  
  40.     def is_inside(self, r):
  41.         if self.left >= r.left and self.right <= r.right and self.top <= r.top and self.bottom <= r.bottom:
  42.             return True
  43.         return False
  44.  
  45.  
  46. i_1 = [int(item) for item in input().split(" ")]
  47. r1 = Rect(i_1[0], i_1[1], i_1[2], i_1[3])
  48.  
  49. i_2 = [int(item) for item in input().split(" ")]
  50. r2 = Rect(i_2[0], i_2[1], i_2[2], i_2[3])
  51.  
  52. if r1.is_inside(r2):
  53.     print("Inside")
  54. else:
  55.     print("Not inside")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement