Rectangles
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19299 Accepted Submission(s): 6255
Problem Description
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
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
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.
Sample Input
1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50
Sample Output
1.00 56.25
Author
seeyou
Source
Recommend
linle | We have carefully selected several similar problems for you: 20572058206220602059
Statistic | Submit | Discuss | Note
这道题乍一看挺复杂,但是是有技巧的。只需要将4个横坐标和4个纵坐标排序然后就可简化成一种很容易计算的形式。还要注意首先要排除没有交叉部分的情况。
View Code1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 5 int main() 6 { 7 double x[4], y[4]; 8 while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2], &x[3], &y[3])) { 9 double minx1 = min(x[0], x[1]), maxx1 = max(x[0], x[1]), minx2 = min(x[2], x[3]), maxx2 = max(x[2], x[3]); 10 double miny1 = min(y[0], y[1]), maxy1 = max(y[0], y[1]), miny2 = min(y[2], y[3]), maxy2 = max(y[2], y[3]); 11 if(minx1 >= maxx2 || maxx1 <= minx2 || maxy1 <= miny2 || miny1 >= maxy2) printf("%.2lf ", 0); 12 else { 13 sort(x, x + 4); 14 sort(y, y + 4); 15 printf("%.2lf ", (x[2] - x[1]) * (y[2] - y[1])); 16 } 17 } 18 }