fork download
  1. #include <cstdio>
  2. #include <algorithm>
  3. //sort pairs of coordinates by value
  4. void sbv(double &x1, double &x2, double &y1, double &y2)
  5. {
  6. if( x1 > x2 ) std::swap( x1 , x2 );
  7. if( y1 > y2 ) std::swap( y1 , y2 );
  8. }
  9. //find whether the rectangles intersect
  10. bool intersect( double b1, double e1, double b2, double e2 )
  11. {
  12. //"b" for "beginning", "e" for "end" (imagine the line segments on the coordinate axis)
  13. if( b2 >= e1 || e2 <= b1 ) return false;
  14. return true;
  15. }
  16.  
  17. int main()
  18. {
  19. double a, b, c, d; //meaningful coodinates of rectangle sides
  20. int n; scanf( "%d", &n );
  21. scanf( "%lg %lg %lg %lg", &a, &b, &c, &d );
  22. double x1 = a, x2 = b, y1 = c, y2 = d; //"x" and "y" pairs are for rectangle of common area
  23. for( int i=1; i<n; i++ )
  24. {
  25. scanf( "%lg %lg %lg %lg", &a, &b, &c, &d ); //refresh the coords.
  26. if( intersect( x1, x2, a, b ) && intersect( y1, y2, c, d ) ) //if they intersect
  27. {
  28. sbv( a, x1, x2, b ); //sort again to find the inner points
  29. sbv( c, y1, y2, d ); //(image is attached)
  30. printf("Common area of %d rectangles S = %lg\n", i+1, (y2-y1)*(x2-x1) );
  31. }
  32. else
  33. {
  34. printf("System of rectangles has no common area.\n");
  35. break;
  36. }
  37.  
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3300KB
stdin
2 
-5 -3 1 4
2 0 1 4

2 
0 4.5 1.2 4.6
2 4 5 7.4

2
2 4 2 4
2 4 2 4

3
2 5 1 4
0 3 2 3
6 7 3 5

4
0 7 0 3
2 6 1 5
3 5 2 5
4 8 2 3
stdout
System of rectangles has no common area.