Advertisement
xerpi

P56635 Rectangles (1)

Nov 27th, 2013
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. /* Copyright (c) 2013   by xerpi */
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. struct Rectangle {
  7.     int x_esq, x_dre, y_baix, y_dalt;
  8. };
  9.  
  10. void llegeix(Rectangle& r);
  11. int relacio(const Rectangle& r1, const Rectangle& r2);
  12.  
  13. int main() {
  14.     int n;
  15.     cin >> n;
  16.     for (int i = 0; i < n; ++i) {
  17.         Rectangle r1, r2;
  18.         llegeix(r1);
  19.         llegeix(r2);
  20.         int resultat = relacio(r1, r2);
  21.         if (resultat == 0)
  22.             cout << "els rectangles no intersecten" << endl;
  23.         else if(resultat == 1)
  24.             cout << "el primer rectangle es interior al segon" << endl;
  25.         else if (resultat == 2)
  26.             cout << "el segon rectangle es interior al primer" << endl;
  27.         else if (resultat == 3)
  28.             cout << "els rectangles intersecten" << endl;
  29.         else
  30.             cout << "els rectangles son iguals" << endl;
  31.     }
  32. }
  33.  
  34.  
  35. int relacio(const Rectangle& r1, const Rectangle& r2)
  36. {
  37.     if (r1.x_esq == r2.x_esq and r1.x_dre == r2.x_dre and
  38.         r1.y_baix == r2.y_baix and r1.y_dalt == r2.y_dalt) {
  39.             return 4;
  40.     }    
  41.    
  42.     if ((r1.x_esq >= r2.x_esq) and (r1.x_dre <= r2.x_dre) and
  43.         (r1.y_baix >= r2.y_baix) and (r1.y_dalt <= r2.y_dalt)) {
  44.             return 1;
  45.     } else if ((r2.x_esq >= r1.x_esq) and (r2.x_dre <= r1.x_dre) and
  46.         (r2.y_baix >= r1.y_baix) and (r2.y_dalt <= r1.y_dalt)) {
  47.             return 2;
  48.      } else {
  49.         if ((r1.x_dre < r2.x_esq) or (r1.y_baix > r2.y_dalt) or
  50.             (r1.x_esq > r2.x_dre) or (r1.y_dalt < r2.y_baix)) {
  51.                 return 0;
  52.         } else {
  53.             return 3;
  54.         }
  55.     }
  56. }
  57.  
  58. void llegeix(Rectangle& r)
  59. {
  60.     cin >> r.x_esq >> r.x_dre >> r.y_baix >> r.y_dalt;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement