csansoon

P9.08 P82952 Rectangles (2)

Dec 12th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. struct Rectangle {
  6.         int x_esq, x_dre, y_baix, y_dalt;
  7. };
  8.  
  9. void llegeix(Rectangle& r) {
  10.         cin >> r.x_esq >> r.x_dre >> r.y_baix >> r.y_dalt;
  11. }
  12.  
  13. int relacio(const Rectangle& r1, const Rectangle& r2) {
  14.         if (r1.x_esq == r2.x_esq && r1.x_dre == r2.x_dre && r1.y_baix == r2.y_baix && r1.y_dalt == r2.y_dalt) return 4;
  15.         if (r2.x_esq <= r1.x_esq && r2.x_dre >= r1.x_dre && r2.y_baix <= r1.y_baix && r2.y_dalt >= r1.y_dalt) return 1;
  16.         if (r1.x_esq <= r2.x_esq && r1.x_dre >= r2.x_dre && r1.y_baix <= r2.y_baix && r1.y_dalt >= r2.y_dalt) return 2;
  17.         if (r1.y_baix >= r2.y_dalt or r2.y_baix >= r1. y_dalt or r1.x_esq >= r2.x_dre or r2.x_esq >= r1.x_dre) return 0;
  18.         else return 3;
  19. }
  20.  
  21. int main() {
  22.         int n;
  23.         while (cin >> n && n != 0)  {
  24.                 Rectangle r1, r2;
  25.                 llegeix(r2);
  26.                 --n;
  27.                 bool intersec = true;
  28.                 while (intersec && n != 0) {
  29.                         r1.x_esq = r2.x_esq;
  30.                         r1.x_dre = r2.x_dre;
  31.                         r1.y_baix = r2.y_baix;
  32.                         r1.y_dalt = r2.y_dalt;
  33.                         llegeix(r2);
  34.                         if (relacio(r1, r2) == 0) intersec = false;
  35.                         else if (relacio(r1, r2) == 1) {
  36.                                 r2.x_esq = r1.x_esq;
  37.                                 r2.x_dre = r1.x_dre;
  38.                                 r2.y_dalt = r1.y_dalt;
  39.                                 r2.y_baix = r1.y_baix;
  40.                         }
  41.                         else if (relacio(r1, r2) == 3) {
  42.                                 if (r1.x_esq > r2.x_esq) r2.x_esq = r1.x_esq;
  43.                                 if (r1.x_dre < r2.x_dre) r2.x_dre = r1.x_dre;
  44.                                 if (r1.y_baix > r2.y_baix) r2.y_baix = r1.y_baix;
  45.                                 if (r1.y_dalt < r2.y_dalt) r2.y_dalt = r1.y_dalt;
  46.                         }
  47.                         //relacio(r1, r2) == 4 || relacio(r1, r2) == 2 es queda igual
  48.                         --n;
  49.                 }
  50.                 if (not intersec) {
  51.                   cout << "empty intersection" << endl;
  52.                   while (n != 0) {
  53.                     llegeix(r2);
  54.                     --n;
  55.                   }
  56.                 }
  57.                 else {
  58.                         cout << "bottom left point = (" << r2.x_esq << ", " << r2.y_baix << ");";
  59.                         cout << " top right point = (" << r2.x_dre << ", " << r2.y_dalt << ")" << endl;
  60.                 }
  61.         }
  62. }
  63.  
  64. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment