http://acm.hdu.edu.cn/showproblem.php?pid=2056
分析:
s1:将矩形对角顶点统一转为左下角和右上角;
s2:求出相交矩形的左下角和右下角坐标
s3:求相交部分面积(有可能不存在)
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
double x1,y1,x2,y2;
double x3,y3,x4,y4;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4){
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
if(x3>x4) swap(x3,x4);
if(y3>y4) swap(y3,y4);
x1=x1>x3 ?x1 :x3;
x2=x2<x4 ? x2: x4;
y1=y1>y3 ? y1:y3;
y2=y2<y4 ? y2:y4;
/*x1=max(x1,x3);
y1=max(y1,y3);
x2=min(x2,x4);
y2=min(y2,y4);*/
printf("%.2lf\n",x1<x2&&y1<y2 ? (x2-x1)*(y2-y1):0);
}
return 0;
}