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
Source
POJ Monthly–2006.04.28, Dagger@PKU_RPWT
贼恶心的题
①情况复杂
②卡精度
③ p o j poj poj 用 g g g++ 和 % l f \%lf %lf会挂
//#include
#include
#include
#include
#include
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define ll long long
const double eps = 1e-8;
struct point{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
};
struct line{point p,v;};
int dcmp(double x){
if(fabs(x) < eps) return 0; else if(x < 0) return -1;else return 1;
}
point operator - (point a,point b){return point(a.x-b.x,a.y-b.y);}
point operator * (point a,double b){return point(a.x*b,a.y*b);}
point operator + (point a,point b){return point(a.x+b.x,a.y+b.y);}
inline double cos(point a,point b){return a.x*b.y-a.y*b.x;}
point get_insert(point a,point v,point b,point u){
point w = b-a; double t = cos(u,w)/cos(u,v);
return a+v*t;
}
double get_ans(point a,point b,point c){
double rate;
if( dcmp(b.y-a.y) != 1 || dcmp(c.y-a.y) != 1 ) return 0;
else {
int ck = dcmp( cos( point(0,1) , b-a ) * cos(point(0,1) , c-a ) );
if(ck == -1 || ck == 0) {
if( (b-a).y > (c-a).y ) swap(b,c);
rate = (b-a).y / (c-a).y;
c = (c-a)*rate+a;
return fabs(cos(b-a,c-a));
}
else {
ck = dcmp( cos(point(0,1),b-a) );
if(ck == 1){
ck = dcmp( cos(b-a,c-a) );
if(ck == 1) swap(b,c);
ck = dcmp(b.x-c.x);
if(ck == 1 || ck == 0) return 0;
else {
if(c.y < b.y) swap(b,c);
rate = (b-a).y/(c-a).y;
c = (c-a)*rate+a;
return fabs(cos(b-a,c-a));
}
}
else {
ck = dcmp( cos(b-a,c-a) );
if(ck == -1) swap(b,c);
ck = dcmp(b.x-c.x);
if(ck == -1 || ck == 0) return 0;
else {
if(c.y < b.y) swap(b,c);
rate = (b-a).y/(c-a).y;
c = (c-a)*rate+a;
return fabs(cos(b-a,c-a));
}
}
}
}
}
void get_line(line &a){scanf("%lf%lf",&a.p.x,&a.p.y); point b; scanf("%lf%lf",&b.x,&b.y); a.v = b - a.p;}
int cas;
void work(){
cas++;
line A,B;get_line(A);get_line(B);
point c = get_insert(A.p,A.v,B.p,B.v),a = B.p,b = B.p+B.v;
double ans = 0;
if( dcmp(min(a.x,b.x)-c.x)==1 || dcmp(c.x-max(a.x,b.x))==1
|| dcmp(min(a.y,b.y)-c.y)==1 || dcmp(c.y-max(a.y,b.y))==1) {printf("0.00\n");return;} a=A.p;b=a+A.v;
if( dcmp(min(a.x,b.x)-c.x)==1 || dcmp(c.x-max(a.x,b.x))==1
|| dcmp(min(a.y,b.y)-c.y)==1 || dcmp(c.y-max(a.y,b.y))==1) {printf("0.00\n");return;}
b = B.p; ans = max(ans,get_ans(c,a,b));
b = B.p+B.v; ans = max(ans,get_ans(c,a,b));
a = A.p+A.v; ans = max(ans,get_ans(c,a,b));
b = B.p; ans = max(ans,get_ans(c,a,b));
printf("%.2lf\n",ans/2.0+eps);
}
int main(){
int T;scanf("%d",&T);
while(T--) work();
return 0;
}