An Easy Problem?!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10833 | Accepted: 1631 |
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.
![](https://i-blog.csdnimg.cn/blog_migrate/e16138e52dbc31bae3615e02aeaaa0d3.gif)
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, x 1, y 1, x 2, y 2, x 3, y 3, x 4, y 4. ( x 1, y 1), ( x 2, y 2) are the endpoints of one board, and ( x 3, y 3), ( x 4, y 4) are the endpoints of the other one.
Each test case consists of 8 integers not exceeding 10,000 by absolute value, x 1, y 1, x 2, y 2, x 3, y 3, x 4, y 4. ( x 1, y 1), ( x 2, y 2) are the endpoints of one board, and ( x 3, y 3), ( x 4, y 4) 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
注:1、判断两条线段是否相交,不相交则接不到雨水,若相交,进行第二步
2、是否有一条线段是水平的,若是,接不到,若不是,进行第三步
3、判断两条线段组成的图形是否是倒‘V’型的,若是,接不到,若不是,进行第四步
4、判断靠上的线段在竖直方向上是否会遮住靠下线段,若遮住,接不到,若遮不住,进行第五步
5、求解两线段的交点
6、求解出接满雨水时的水平线两个端点,其中有一个是两条线段最高点中较低的一个,另一个用数学公式求出即可
7、根据求面积公式求出三角形的面积,此题比较好求
比较神的一个题,写了两天╮(╯▽╰)╭
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define eps 1e-8
#define inf 0x3f3f3f3f
struct point{
double x,y;
}p1,p2,p3,p4;
double min(double a,double b){
return a>b?b:a;
}
int dblcmp(double x){
if( fabs(x) < eps )
return 0;
return x > 0 ? 1 : -1;
}
double dot(double x1,double y1,double x2,double y2){
return x1*y2-y1*x2;
}
double cro(point a,point b,point c){
return dot( b.x-a.x , b.y-a.y , c.x-a.x , c.y-a.y );
}
bool on(point a,point b,point c){
double xmin,xmax,ymin,ymax;
if( a.x < b.x ){
xmin = a.x;
xmax = b.x;
}
else{
xmin = b.x;
xmax = a.x;
}
if( a.y < b.y ){
ymin = a.y;
ymax = b.y;
}
else{
ymin = b.y;
ymax = a.y;
}
if( c.x < xmin || c.x > xmax || c.y < ymin || c.y > ymax )
return false;
return true;
}
bool crossok(point a,point b,point c,point d){ //线段是否相交
if( a.y-b.y == 0 || c.y-d.y == 0 ) //有一线段水平
return false;
if( a.x-b.x==0 && c.x-d.x==0 ) //两线段竖直
return false;
if( a.x-b.x!=0 && c.x-d.x!=0 ){ //斜率存在
double k11 = (a.y-b.y)/(a.x-b.x);
double k22 = (c.y-d.y)/(c.x-d.x);
if( fabs(k11-k22) < eps ) //斜率相同
return false;
}
double d1 = cro(c,d,a);
double d2 = cro(c,d,b);
double d3 = cro(a,b,c);
double d4 = cro(a,b,d);
if( d1*d2 < 0 && d3*d4 < 0 )
return true;
if( d1 == 0 && on(c,d,a) )
return true;
if( d2 == 0 && on(c,d,b) )
return true;
if( d3 == 0 && on(a,b,c) )
return true;
if( d4 == 0 && on(a,b,d) )
return true;
return false;
}
bool equ(point a,point b){
if( a.x==b.x && a.y==b.y )
return true;
return false;
}
point is(point a,point b,point c,point d){ //求交点
if( equ(a,c) || equ(a,d) )
return a;
if( equ(b,c) || equ(b,d) )
return b;
point q;
double k1,k2,b1,b2;
if( a.x-b.x==0 ){ //ab线段竖直
q.x = a.x;
k2 = (c.y-d.y) / (c.x-d.x);
b2 = c.y - c.x*k2;
q.y = k2 * q.x + b2;
}
else if( c.x-d.x==0 ){ //cd线段竖直
q.x = c.x;
k1 = (a.y-b.y) / (a.x-b.x);
b1 = a.y - a.x*k1;
q.y = k1 * q.x + b1;
}
else{ //其他
k1 = (a.y-b.y) / (a.x-b.x);
k2 = (c.y-d.y) / (c.x-d.x);
b1 = a.y - a.x*k1;
b2 = c.y - c.x*k2;
q.x = (b2-b1) / (k1-k2);
q.y = q.x * k1 + b1;
}
return q;
}
bool nonono(point a,point b,point c,point d,point e){ //图形呈倒‘V’型
if( equ(a,e) || equ(c,e) )
return true;
return false;
}
bool overlop(point a,point b,point c,point d,point e){ //呈斜‘V’型,且上边沿盖过下边沿
if( a.x == b.x || c.x == d.x )
return false;
double k1 = (a.y-b.y)/(a.x-b.x);
double k2 = (c.y-d.y)/(c.x-d.x);
if( e.x < a.x && e.x < c.x ){ //右斜
if( a.x >= c.x && a.y > c.y && k1 > k2 )
return true;
if( a.x <= c.x && a.y < c.y && k2 > k1 )
return true;
}
else if( e.x > a.x && e.x > c.x ){ //左斜
if( a.x <= c.x && a.y > c.y && k2 > k1 )
return true;
if( a.x >= c.x && a.y < c.y && k1 > k2 )
return true;
}
return false;
}
double getarea(point a,point b,point c,point d,point e){
double w,u,dis; //printf("point %.2lf %.2lf\n",e.x,e.y);
double k1,k2,b1,b2;
if( a.y < c.y ){
if( c.x == d.x ){
dis = fabs(a.x-c.x);
}
else{
k2 = (c.y-d.y)/(c.x-d.x);
b2 = c.y-k2*c.x;
dis = fabs( a.x-(a.y-b2)/k2 );
}
}
else{
if( a.x == b.x ){
dis = fabs(a.x-c.x);
}
else{
k1 = (a.y-b.y)/(a.x-b.x);
b1 = a.y-k1*a.x;
dis = fabs( c.x - (c.y-b1)/k1 );
}
}
//printf("k1 %.2lf k2 %.2lf\n",k1,k2);
//printf("asddasdasd %.2lf\n",dis);
w = fabs(min(a.y,c.y) - e.y);
return w*dis/2;
}
void solve(point a,point b,point c,point d){
point e = is(a,b,c,d);
//printf("%.3lf %.3lf\n",e.x,e.y);
if( nonono(a,b,c,d,e) )
printf("0.00\n");
else{
if( overlop(a,b,c,d,e) )
printf("0.00\n");
else{
printf("%.2lf\n",getarea(a,b,c,d,e));
}
}
}
int main(){
int t;
cin>>t;
while(t--){
cin>>p1.x>>p1.y>>p2.x>>p2.y;
cin>>p3.x>>p3.y>>p4.x>>p4.y;
point w;
if( p1.y < p2.y ){
w = p1;
p1 = p2;
p2 = w;
}
if( p3.y < p4.y ){
w = p3;
p3 = p4;
p4 = w;
}
if( crossok(p1,p2,p3,p4) ){
solve(p1,p2,p3,p4);
}
else{
cout<<"0.00\n";
}
}
return 0;
}