http://acm.hdu.edu.cn/showproblem.php?pid=2056
很水的一道计算几何的题
但是自己逻辑不是很清楚
直接放弃判断点的相对位置来解题
解题思路
总体思路:将矩形相交转化为x.y区间相交
判断矩形相离与否 若不相离则取区间相交的两个值
而判定相离条件为x,y区间均相交
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef struct{
double x,y;
}point;
int judge(double x1,double x2,double x3,double x4){
int ret = 1;
if(x2<=x3||x4<=x1)
ret = 0;
return ret;
}
int main()
{
double x[4] = {0};
double y[4] = {0};
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])!=EOF){
double ans = 0.0;
double tx1,tx2,tx3,tx4,ty1,ty2,ty3,ty4;
tx1 = min(x[0],x[1]);
tx2 = max(x[0],x[1]);
tx3 = min(x[2],x[3]);
tx4 = max(x[2],x[3]);
ty1 = min(y[0],y[1]);
ty2 = max(y[0],y[1]);
ty3 = min(y[2],y[3]);
ty4 = max(y[2],y[3]);
int flag = 0;
//区间是否相交
if(judge(tx1,tx2,tx3,tx4)&&judge(ty1,ty2,ty3,ty4))
flag = 1;
//排序找到第二第三的x,y
sort(x,x+4);
sort(y,y+4);
if(flag)
ans = (x[2] - x[1]) *(y[2] - y[1]);
printf("%.2lf\n",ans);
}
return 0;
}