CPP   17

Overlap area1.cpp

Guest on 6th June 2021 05:13:55 PM

  1. #include <stdio.h>
  2. int max(int a,int b,int c)
  3. {
  4.     if(a>=b&&a>=c)
  5.     {
  6.         return a;  
  7.     }
  8.     if(b>=a&&b>=c)
  9.     {
  10.         return b;  
  11.     }
  12.     if(c>=a&&c>=b)
  13.     {
  14.         return c;  
  15.     }  
  16. }
  17. int min(int a,int b,int c)
  18. {
  19.     if(a<=b&&a<=c)
  20.     {
  21.         return a;  
  22.     }
  23.     if(b<=a&&b<=c)
  24.     {
  25.         return b;  
  26.     }
  27.     if(c<=a&&c<=b)
  28.     {
  29.         return c;  
  30.     }  
  31. }
  32. int overlap(int r1x1,int r1y1,int r1x2,int r1y2,int r2x1,int r2y1,int r2x2,int r2y2,int r3x1,int r3y1,int r3x2,int r3y2)
  33. {
  34.     int x,y;
  35.     x=min(r1x2,r2x2,r3x2)-max(r1x1,r2x1,r3x1);
  36.     y=min(r1y2,r2y2,r3y2)-max(r1y1,r2y1,r3y1);
  37.     if(x>0&&y>0)
  38.     return x*y;
  39.     else
  40.     return 0;
  41. }
  42. int rectangle(int r1x1,int r1y1,int r1x2,int r1y2)
  43. {
  44.     int x,y;
  45.     x=r1x2-r1x1;
  46.     y=r1y2-r1y1;
  47.     return x*y;
  48. }
  49. int main()
  50. {
  51.     int r1x1,r1y1,r1x2,r1y2,r2x1,r2y1,r2x2,r2y2,r3x1,r3y1,r3x2,r3y2,o1,o2,o3,o4,r1,r2,r3,area;
  52.     scanf("%d%d%d%d%d%d%d%d%d%d%d%d",&r1x1,&r1y1,&r1x2,&r1y2,&r2x1,&r2y1,&r2x2,&r2y2,&r3x1,&r3y1,&r3x2,&r3y2); 
  53.     o1=overlap(r1x1,r1y1,r1x2,r1y2,r2x1,r2y1,r2x2,r2y2,0,0,20000,20000);
  54.     o2=overlap(r3x1,r3y1,r3x2,r3y2,r2x1,r2y1,r2x2,r2y2,0,0,20000,20000);
  55.     o3=overlap(r1x1,r1y1,r1x2,r1y2,r3x1,r3y1,r3x2,r3y2,0,0,20000,20000);
  56.     o4=overlap(r1x1,r1y1,r1x2,r1y2,r2x1,r2y1,r2x2,r2y2,r3x1,r3y1,r3x2,r3y2);
  57.     r1=rectangle(r1x1,r1y1,r1x2,r1y2);
  58.     r2=rectangle(r2x1,r2y1,r2x2,r2y2);
  59.     r3=rectangle(r3x1,r3y1,r3x2,r3y2);
  60.     area=r1+r2+r3-o1-o2-o3+o4;
  61.     printf("%d\n",area);
  62. }

Raw Paste


Login or Register to edit or fork this paste. It's free.