csansoon

P9.07 P56635 Rectangles (1)

Dec 12th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Rectangle {
  5.     int x_left, x_right, y_down, y_up;
  6. };
  7.  
  8. void read(Rectangle& r) {
  9.     cin >> r.x_left >> r.x_right >> r.y_down >> r.y_up;
  10. }
  11.  
  12. int relationship(const Rectangle& r1, const Rectangle& r2) {
  13.     if (r1.x_left == r2.x_left && r1.x_right == r2.x_right && r1.y_down == r2.y_down && r1.y_up == r2.y_up) return 4;
  14.     if (r2.x_left <= r1.x_left && r2.x_right >= r1.x_right && r2.y_down <= r1.y_down && r2.y_up >= r1.y_up) return 1;
  15.     if (r1.x_left <= r2.x_left && r1.x_right >= r2.x_right && r1.y_down <= r2.y_down && r1.y_up >= r2.y_up) return 2;
  16.     if (r1.y_down > r2.y_up or r2.y_down > r1. y_up or r1.x_left > r2.x_right or r2.x_left > r1.x_right) return 0;
  17.     else return 3;
  18. }
  19.  
  20. int main() {
  21.     int n;
  22.     Rectangle r1, r2;
  23.     cin >> n;
  24.     while (n != 0) {
  25.         read(r1);
  26.         read(r2);
  27.         int i=relationship(r1,r2);
  28.         if (i == 0) cout <<  "rectangles do not intersect" << endl;
  29.         else if (i == 1) cout << "the first rectangle is inside the second one" << endl;
  30.         else if (i == 2) cout << "the second rectangle is inside the first one" << endl;
  31.         else if (i == 3) cout << "rectangles intersect" << endl;
  32.         else if (i == 4) cout << "rectangles are identical" << endl;
  33.         --n;
  34.     }
  35. }
  36.  
  37. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment