JosepRivaille

P56635: Rectangles (1)

May 7th, 2015
1,758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 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. //Pre:
  22. //Post:
  23. int main() {
  24.     int n;
  25.     Rectangle r1, r2;
  26.     cin >> n;
  27.     while (n != 0) {
  28.         llegeix(r1);
  29.         llegeix(r2);
  30.         if (relacio(r1, r2) == 0) cout <<  "els rectangles no intersecten" << endl;
  31.         else if (relacio(r1, r2) == 1) cout << "el primer rectangle es interior al segon" << endl;
  32.         else if (relacio(r1, r2) == 2) cout << "el segon rectangle es interior al primer" << endl;
  33.         else if (relacio(r1, r2) == 3) cout << "els rectangles intersecten" << endl;
  34.         else if (relacio(r1, r2) == 4) cout << "els rectangles son iguals" << endl;
  35.         --n;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment