hdu2056rectangles

来源:互联网 发布:工业控制软件系统 编辑:程序博客网 时间:2024/06/08 11:56

Rectangles

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9863    Accepted Submission(s): 3144


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.005.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50
 

Sample Output
1.0056.25

刚刚看到这道题,想用斜率的乘积把四种情况分个类

后来发现如果用比较大小来转化,把每个矩形得两个坐标都转化成左上右下,然后就只剩下两种情况了

1矩形不相交  ,面积为0  2矩形相交算面积,代码如下

#include <stdio.h>double max(double a,double b){double temp;if(a<b) {temp=a;a=b;b=temp;}return a;}double min(double a,double b){double temp;if(a>b) {temp=a;a=b;b=temp;}return a;}int main(){double x1,y1,x2,y2,x3,y3,x4,y4,temp;double s;while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)){double s=0;if(x1<x2) temp=x1,x1=x2,x2=temp;if(y1<y2) temp=y1,y1=y2,y2=temp;if(x3<x4) temp=x3,x3=x4,x4=temp;if(y3<y4) temp=y3,y3=y4,y4=temp;//把两个矩形已知点 变成右上左下,计算面积是方便统计if(x2>x3||x1<x4||y1<y4||y2>y3) //如果满足上面条件,两个矩形不相交 s=0;else s=(min(x1,x3)-max(x2,x4))*(min(y1,y3)-max(y2,y4)); printf("%.2lf\n",s); }return 0;}


原创粉丝点击