POJ - 2826 An Easy Problem?! 计算几何

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

题意:两线段能装多少水

题解:这操蛋的题,注意结果加个eps,直接fabs也过不了.... ,本身不难,多分几种情况就行了,下面有几个数据先试一下

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define eps 1e-8
struct node{
	double x;
	double y;
}; 
node a,b,c,d;
bool judge(node a,node b,node c,node d) // 判断平行 
{
	
	double u,v,w,z;
        u=b.x-a.x;
        v=b.y-a.y;
        w=d.x-c.x;
        z=d.y-c.y;
        if(fabs(u/w*z-v)<=eps) return true;
	
	return false;
}
bool judge1(node a,node b,node c,node d)// 判断两线段是否相交 
{
	if(min(a.x,b.x) <= max(c.x,d.x) && min(c.x,d.x) <= max(a.x,b.x) && min(a.y,b.y) <= max(c.y,d.y) &&min(c.y,d.y)<=max(a.y,b.y))
	{
		
		double u,v,w,z;
                u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
                v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
                w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
                z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
        
                return (u*v<=0.00000001 && w*z<=0.00000001);
	}
	return false;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		cin >> a.x >> a.y;
		cin >> b.x >> b.y;
		cin >> c.x >> c.y;
		cin >> d.x >> d.y;
		//cout<<judge1(a,b,c,d)<<endl;
		if(!judge1(a,b,c,d)||judge(a,b,c,d)||(c.y==d.y)||(a.y==b.y)) // 
		{
			printf("0.00\n");
		}
		else if(a.x==b.x)  // 有一条是竖着 
		{
			double k2=(d.y-c.y)/(d.x-c.x);
			double b2=c.y-(d.y-c.y)/(d.x-c.x)*c.x;
			double y=k2*a.x+b2;
			double yy=min(max(a.y,b.y),max(c.y,d.y));
			double xx=(yy-b2)/k2;
			if(yy<=y)
			{
				printf("0.00\n");
				continue;
			}
			printf("%.2f\n",fabs(fabs(xx-a.x)*fabs(yy-y)*0.5)+eps);
		}
		else if(c.x==d.x)  // 有一条是竖着 
		{
			double k1=(b.y-a.y)/(b.x-a.x);
			double b1=a.y-(b.y-a.y)/(b.x-a.x)*a.x;
			double y=k1*c.x+b1;
			double yy=min(max(a.y,b.y),max(c.y,d.y));
			double xx=(yy-b1)/k1;
			if(yy<=y)
			{
				printf("0.00\n");
				continue;
			}
			printf("%.2f\n",fabs(fabs(xx-c.x)*fabs(yy-y)*0.5)+eps);
		}
		else
		{
		//	cout<<1<<endl;
			double k1=(b.y-a.y)/(b.x-a.x);
			double k2=(d.y-c.y)/(d.x-c.x);
		//	cout<<k1<<" "<<k2<<endl;
			double b1=a.y-(b.y-a.y)/(b.x-a.x)*a.x;
			double b2=c.y-(d.y-c.y)/(d.x-c.x)*c.x;
			double x=(b2-b1)/(k1-k2);
			double y=k1*x+b1;
			double yy;
			int flag=-1;
			if(max(a.y,b.y)>max(c.y,d.y))
			{
				flag=1;
				yy=max(c.y,d.y);
			}
			else
			{
				flag=0;
				yy=max(a.y,b.y);
			}
			
			double x_1=(yy-b1)/k1,x_2=(yy-b2)/k2;
			if(k2>0&&k1>0&&k2>k1&&max(a.x,b.x)<=max(c.x,d.x))
			{
				printf("0.00\n");
				continue;
			}
			if(k2>0&&k1>0&&k2<k1&&max(a.x,b.x)>=max(c.x,d.x))
			{
				printf("0.00\n");
				continue;
			}
			if(k2<0&&k1<0&&k2<k1&&min(a.x,b.x)>=min(c.x,d.x))
			{
				printf("0.00\n");
				continue;
			}
			if(k2<0&&k1<0&&k2>k1&&min(a.x,b.x)<=min(c.x,d.x))
			{
				printf("0.00\n");
				continue;
			}	
			
			if(fabs(yy-y)<=eps)
			{
				printf("0.00\n");
			}
			else 
			printf("%.2f\n",fabs(fabs(yy-y)*0.5*fabs((yy-b1)/k1-(yy-b2)/k2))+eps);
		}
	}
	
	return 0;
}
/*
6259 2664 8292 9080 1244 2972 9097 9680
6162.65

0 1 1 0
1 0 2 1
1.00

0 1 2 1
1 0 1 2
0.00

0 0 10 10
0 0 9 8
0.00

0 0 10 10
0 0 8 9
4.50

0.9 3.1 4 0
0 3 2 2
0.50

0 0 0 2
0 0 -3 2
3.00

1 1 1 4
0 0 2 3
0.75

1 2 1 4
0 0 2 3
0.00

1 1 7 7 
7 7 15 15
0.00

0 0 -1 -1
0 0 1 -1
0.00
*/ 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值