刚开始想的是直接分各种情况考虑,发现情况太多,比较麻烦,来不如将两个矩形的坐标从新选择,就能方便很多
思路:
确定每个矩阵副对角线上的两个坐标,若两个矩形相交就肯定有x1<x2&&y1<y2
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
void change(double *x,double *y);
double max(double a,double b);
double min(double a,double b);
double x1,x2,x3,x4,y1,y2,y3,y4,s;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4)
{
if(x1>x2) change(&x1,&x2);
if(y1>y2) change(&y1,&y2);
if(x3>x4) change(&x3,&x4);
if(y3>y4) change(&y3,&y4);
x1=max(x1,x3);
y1=max(y1,y3);
x2=min(x2,x4);
y2=min(y2,y4);
if(x1<x2&&y1<y2)
{
s=(x2-x1)*(y2-y1);
}
else
s=0;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<s<<endl;
}
}
void change(double *x,double *y)
{
double t;
t=*x;*x=*y;*y=t;
}
double max(double a,double b)
{
if(a>b)
return a;
else
return b;
}
double min(double a,double b)
{
if(a<b)
return a;
else
return b;
}