Rectangles
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
思路
先分别确定x1,x2,x3,x4 y1,y2,y3,y4的大小
再判断是否相交
如果相交则进行排序中间的连个数就是相交的两个坐标
求得相交的宽和长即可求出相交的面积是多少
AC代码
#include <stdio.h>
int main(void)
{
double x[10], y[10], t;
int i, j;
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x[1],&y[1],&x[2],&y[2],&x[3],&y[3],&x[4],&y[4]) != EOF){
if(x[2]<x[1]){ //保证x2 > x1;
t = x[2];
x[2] = x[1];
x[1] = t;
}
if(x[4]<x[3]){ //保证x4 > x3;
t = x[4];
x[4] = x[3];
x[3] = t;
}
if(y[2]<y[1]){ //保证y2 > y1;
t = y[2];
y[2] = y[1];
y[1] = t;
}
if(y[4]<y[3]){ //保证y4 > y3;
t = y[4];
y[4] = y[3];
y[3] = t;
}
if(x[1]<x[4]&&x[2]>x[3]&&y[1]<y[4]&&y[2]>y[3]){ //如果相交
for(i = 1; i < 5; i ++){
for(j = 4; j > i; j--){
if(x[i] > x[j]){
t = x[i];
x[i] = x[j];
x[j] = t;
}
}
}
for(i = 1; i < 5; i ++){
for(j = 4; j > i; j--){
if(y[i] > y[j]){
t = y[i];
y[i] = y[j];
y[j] = t;
}
}
}
printf("%.2f\n",(x[3] - x[2]) * (y[3] - y[2])); //始终取这四个数从小到大排的中间两个数;
}
else printf("0.00\n");
}
return 0;
}