POJ2826 An Easy Problem?!

这是一道细节题。
题目中让我们用两条线段接雨水,雨水是垂直落下的,问我们用给定的两条线段能接到多少水。
这里用很多种情况都要一一讨论。
1:两条线段不想交的时候接不到雨水
2:两条线段重合的时候接不到雨水
3:两条线段的交点与期中一条线段的最高点相同时,无法接到雨水
4:有一条线段水平时接不到雨水。
5:当两条线段的最高点均在交点一侧时,期中较高的点遮住了较低的点时,无法接住雨水。
 
注:最后计算面积的时候取点十分重要。我程序中有注释。在这个地方我贡献了5,6次才发现。之前把各种接不到的情况考虑后,却不小心改掉了一个地方。。。。。郁闷。。
 
An Easy Problem?!
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6739 Accepted: 969

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, 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
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<complex>
#include<cstring>
#include<string>
#include<iomanip> 



using namespace std;
#define max(a,b)  (a>b?a:b)
#define min(a,b)  (a<b?a:b)
#define eps 1e-8
#define zero(x)   (((x)>0?(x):-(x))<eps)


struct point{double x,y;};
struct line {point a,b;};

int n;
line l1,l2,l3;

double triangleArea(point a,point b,point c)   //求面积 
{
	double k=a.x*b.y+b.x*c.y+c.x*a.y
			-b.x*a.y-c.x*b.y-a.x*c.y;
	return k>0?k/2:-k/2;
}

int sig(double k)  
{  
	if(fabs(k)<eps)
		return 0;
	return (k>0)?1:-1;  
}  
  
double det(double x1, double y1, double x2, double y2)  
{  
    return x1 * y2 - x2 * y1;  
}  

double xmult(point p0,point p1,point p2)  
{  
    return det(p1.x-p0.x,p1.y-p0.y,p2.x-p0.x,p2.y-p0.y);   
}  
  
double dotdet(double x1,double y1,double x2,double y2)  
{  
    return x1*x2+y1*y2;  
}  
  
double dot(point p0, point p1, point p2)  
{  
    return dotdet(p1.x-p0.x,p1.y-p0.y,p2.x-p0.x,p2.y-p0.y);  
}  
  
int between(point p0,point p1, point p2)  
{  
    return sig(dot(p0,p1,p2));  
}  
  
int intersect1(point a, point b, point c, point d) //判断线段相交 
{  
    double s1, s2, s3, s4;  
    int d1 = sig(s1 = xmult(a, b, c));  
    int d2 = sig(s2 = xmult(a, b, d));  
    int d3 = sig(s3 = xmult(c, d, a));  
    int d4 = sig(s4 = xmult(c, d, b));  
    if ((d1^d2) == -2 && (d3^d4) == -2)  
   	{ 
      return 1;  
    }  
    if (d1 == 0 && between(c, a, b) <= 0) return 2;  
    if (d2 == 0 && between(d, a, b) <= 0) return 2;  
    if (d3 == 0 && between(a, c, d) <= 0) return 2;  
    if (d4 == 0 && between(b, c, d) <= 0) return 2;  
    return 0;  
}

bool parallel(const line &u,const line &v)    //判断两条直线是否平行   x1y2-y1x2=0 
{
	return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
}


point intersection(line &u ,line &v)         //求两条直线的交点 
{
	point temp=u.a;
	double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
			 /((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
	temp.x+=(u.b.x-u.a.x)*t;
	temp.y+=(u.b.y-u.a.y)*t;
	return temp;
}

bool hide(line u,line v,point in)   //判断是否有入口被遮住的情况 
{
	point temp1,temp2;
	temp1=(u.a.y>=v.a.y?u.a:v.a);
	temp2=(u.a.y>=v.a.y?v.a:u.a);
	if(temp1.x<=temp2.x && temp2.x<in.x &&xmult(in,temp1,temp2)>eps)
		return true;
	if(temp1.x>=temp2.x && temp2.x>in.x &&xmult(in,temp1,temp2)<eps)
		return true;
	return false;
	
}


void init()
{
	scanf("%lf%lf%lf%lf",&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y);
	scanf("%lf%lf%lf%lf",&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);	
	if(l1.a.y<l1.b.y)
		swap(l1.a,l1.b);
	if(l2.a.y<l2.b.y)
		swap(l2.a,l2.b);
	
}

double solve()
{
	line ll;
	ll.a.x=ll.a.y=0;
	ll.b.x=1;
	ll.b.y=0; 
	point temp,temp2;
	if(parallel(l1,l2))//两条线平行 ,重合 
		return 0.00;
	if(parallel(ll,l2)||parallel(ll,l1))  //某条线水平 
		return 0.00;
	if(intersect1(l1.a,l1.b,l2.a,l2.b)!=1&&intersect1(l1.a,l1.b,l2.a,l2.b)!=2) //两条线不想交 
		return 0.00;
	temp=(l1.a.y<l2.a.y?l1.a:l2.a);
	l3.b=temp;
	temp.x-=10000;
	l3.a=temp;
	temp=intersection(l1,l2);  //交点与较低点一样高,就是平行 和  交点为一块板的较高点 
	if(temp.y>=l1.a.y || temp.y>=l2.a.y)
		return 0.00;
	if(hide(l1,l2,temp))     //较高点不能遮住较低点 
		return 0.00; 
	if(l1.a.y>=l2.a.y)       //选点,注意不要选错,因为两点y相等时选取的最低点是右边的点,所以 计算出的水平线交点应该与左边相交。 
		temp2=intersection(l1,l3);
	else
		temp2=intersection(l2,l3);
	return triangleArea(l3.b,temp,temp2);
}

int main()
{
	
	cin>>n;
	while(n--)
	{
		init();
		cout<<setprecision(2)<<setiosflags(ios::fixed)<<solve()<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值