poj2826:An Easy Problem?!(计算几何)

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<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值