5D - Rectangles

水深无声 2022-01-07 00:01 289阅读 0赞

Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY .

Input

The first line of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle are(x1,y1),(x2,y2);the other two points on the second rectangle are (x3,y3),(x4,y4).

Output

For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.

Sample Input

  1. 1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00
  2. 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50

Sample Output

  1. 1.00
  2. 56.25
  3. // 没考虑无相交区域

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<stdio.h>
  2. 2 int main()
  3. 3 {
  4. 4 double x1,y1, x2,y2, x3,y3, x4,y4, a,b, t;
  5. 5 while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF)
  6. 6 {
  7. 7 if(x1>x2)
  8. 8 { t=x2; x2=x1; x1=t; }
  9. 9 if(y1>y2)
  10. 10 { t=y2; y2=y1; y1=t; }
  11. 11 if(x3>x4)
  12. 12 { t=x4; x4=x3; x3=t; }
  13. 13 if(y3>y4)
  14. 14 { t=y4; y4=y3; y3=t; }
  15. 15 a=x1-x4>x3-x2?x1-x4:x3-x2;
  16. 16 b=y1-y4>y3-y2?y1-y4:y3-y2;
  17. 17 printf("%.2f\n", a*b);
  18. 18 }
  19. 19 return 0;
  20. 20 }

WA

  1. // 还是错得离谱 感觉没有智商T^T

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<stdio.h>
  2. 2 int main()
  3. 3 {
  4. 4 double x1,y1, x2,y2, x3,y3, x4,y4, a,b, t;
  5. 5 while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF)
  6. 6 {
  7. 7 if(x1>x2)
  8. 8 { t=x2; x2=x1; x1=t; }
  9. 9 if(y1>y2)
  10. 10 { t=y2; y2=y1; y1=t; }
  11. 11 if(x3>x4)
  12. 12 { t=x4; x4=x3; x3=t; }
  13. 13 if(y3>y4)
  14. 14 { t=y4; y4=y3; y3=t; }
  15. 15 a=x1-x4>x3-x2?x1-x4:x3-x2;
  16. 16 b=y1-y4>y3-y2?y1-y4:y3-y2;
  17. 17 if(a>=0||b>=0) printf("0.00\n");
  18. 18 else printf("%.2f\n", a*b);
  19. 19 }
  20. 20 return 0;
  21. 21 }

WA*2

  1. //

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<stdio.h>
  2. 2 int main()
  3. 3 {
  4. 4 double x1,y1, x2,y2, x3,y3, x4,y4, a,b, t;
  5. 5 while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF)
  6. 6 {
  7. 7 if(x1>x2)
  8. 8 { t=x2; x2=x1; x1=t; }
  9. 9 if(y1>y2)
  10. 10 { t=y2; y2=y1; y1=t; }
  11. 11 if(x3>x4)
  12. 12 { t=x4; x4=x3; x3=t; }
  13. 13 if(y3>y4)
  14. 14 { t=y4; y4=y3; y3=t; }
  15. 15 a=(x2<x4?x2:x4)-(x1>x3?x1:x3);
  16. 16 b=(y2<y4?y2:y4)-(y1>y3?y1:y3);
  17. 17 if(a<0||b<0) printf("0.00\n");
  18. 18 else printf("%.2f\n", a*b);
  19. 19 }
  20. 20 return 0;
  21. 21 }

AC

转载于:https://www.cnblogs.com/goldenretriever/p/10355783.html

发表评论

表情:
评论列表 (有 0 条评论,289人围观)

还没有评论,来说两句吧...

相关阅读

    相关 %05d和%5d

    %nd 输出的整型宽度至少为n位,右对齐,%5d即宽度至少为5位,位数大于5则输出实际位数 %0nd 用得比较多,表示输出的整型宽度至少为n位,不足n位用0填充 pri