Description
It’s raining outside. Farmer Johnson’s bull Ben wants some rain to water his flowers. Ben nails two wooden boards on the wall of his barn. Shown in the pictures below, the two boards on the wall just look like two segments on the plane, as they have the same width.
Your mission is to calculate how much rain these two boards can collect.
Input
The first line contains the number of test cases.
Each test case consists of 8 integers not exceeding 10,000 by absolute value, x1, y1, x2, y2, x3, y3, x4, y4. (x1, y1), (x2, y2) are the endpoints of one board, and (x3, y3), (x4, y4) are the endpoints of the other one.
Output
For each test case output a single line containing a real number with precision up to two decimal places - the amount of rain collected.
Sample Input
2
0 1 1 0
1 0 2 1
0 1 2 1
1 0 1 2
Sample Output
1.00
0.00
解题思路:
方便起见,每个线段端点 s 在端点 e 上方
0.00的情况:
1.其中一线段斜率为0
2.两线段斜率符号相同,靠近y轴的线段挡住了另一条线段
其他情况:
以两条线段 s y坐标较小的作为高计算面积,用叉积
最后要加个EPS,没过的看一下poj,discuss的数据
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
const double EP=1e-8;
struct POINT{
double x,y;
POINT(double x=0,double y=0):x(x),y(y){}
}cross,p;
struct LINESEG{
POINT s,e;
LINESEG(POINT s=0,POINT e=0):s(s),e(e){}
};
struct LINE{
double a,b,c;
LINE(double a=1,double b=-1,double c=0):a(a),b(b),c(c){}
};
double multiply(POINT sp,POINT ep,POINT op){
return(sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
LINE makeline(POINT p1,POINT p2){//直线一般式
LINE tl;
int sign=1;
tl.a=p2.y-p1.y;
if(tl.a<0){
sign=-1;
tl.a=sign*tl.a;
}
tl.b=sign*(p1.x-p2.x);
tl.c=sign*(p1.y*p2.x-p1.x*p2.y);
return tl;
}
bool online(LINESEG l,POINT p){//在线段上
return((fabs(multiply(l.e,p,l.s))<=EP)&&(((p.x-l.s.x)*(p.x-l.e.x)<=0)&&((p.y-l.s.y)*(p.y-l.e.y)<=0)));
}
bool lineintersect(LINE l1,LINE l2,POINT &p){//直线相交返回交点
double d=l1.a*l2.b-l2.a*l1.b;
if(fabs(d)<=EP)return false;
p.x=(l2.c*l1.b-l1.c*l2.b)/d;
p.y=(l2.a*l1.c-l1.a*l2.c)/d;
return true;
}
bool intersection(LINESEG l1,LINESEG l2,POINT &inter){//线段相交返回交点
LINE ll1,ll2;
ll1=makeline(l1.s,l1.e);
ll2=makeline(l2.s,l2.e);
if(lineintersect(ll1,ll2,inter))return online(l1,inter)&&online(l2,inter);
else return false;
}
double slope(LINE l){//斜率
if(fabs(l.a)<1e-20)return 0;
if(fabs(l.b)<1e-20)return 1e9;
return -(l.a/l.b);
}
int main(){
int t;scanf("%d",&t);
while(t--){
double sx1,sy1,ex1,ey1;scanf("%lf%lf%lf%lf",&sx1,&sy1,&ex1,&ey1);
double sx2,sy2,ex2,ey2;scanf("%lf%lf%lf%lf",&sx2,&sy2,&ex2,&ey2);
POINT sp1=POINT(sx1,sy1),ep1=POINT(ex1,ey1),sp2=POINT(sx2,sy2),ep2=POINT(ex2,ey2);
if(sy1<ey1)swap(sp1,ep1);if(sy2<ey2)swap(sp2,ep2);
LINESEG seg1=LINESEG(sp1,ep1);
LINESEG seg2=LINESEG(sp2,ep2);
if(!intersection(seg1,seg2,cross)||fabs(sy1-ey1)<=EP||fabs(sy2-ey2)<=EP)puts("0.00");
else{
if(multiply(seg2.s,seg1.s,cross)<0)swap(seg1,seg2);
LINE u=makeline(seg1.s,seg1.e),v=makeline(seg2.s,seg2.e);
if(slope(u)*slope(v)>=0&&fabs(seg1.s.x)>=fabs(seg2.s.x)){
puts("0.00");
continue;
}
if(seg1.s.y>seg2.s.y)swap(seg1,seg2);
lineintersect(makeline(seg2.s,seg2.e),makeline(seg1.s,POINT(100000,seg1.s.y)),p);
printf("%.2f\n",fabs(multiply(seg1.s,p,cross))/2.0+EP);
}
}
}