An Easy Problem?! POJ - 2826 (几何)

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 <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <map>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <complex> 
using namespace std;
typedef long long ll;
const double ep = 1e-8;

struct Point{
	double x,y;
	Point(double x1=0,double y1=0){
		x = x1,y = y1;
	}
};

Point operator - (Point a,Point b)
{
	return Point(a.x-b.x,a.y-b.y);
}
Point operator + (Point a,Point b)
{
	return Point(a.x+b.x,a.y+b.y);
}

inline double Dot(Point a,Point b) // 点乘 
{
	return a.x*b.x + a.y*b.y;
}

inline double cross(Point a,Point b) // ×乘 
{
	return a.x*b.y - a.y*b.x;
}
 
int fcmd(double x)   // 三态函数 
{
	if(fabs(x) < 1e-12) return 0;
	return x > 0 ? 1 : -1; 
}

bool seginx(Point a,Point b,Point c,Point d)  // 判断线段相交 
{
	return 
	max(a.x,b.x) >= min(c.x,d.x)&&
	max(a.y,b.y) >= min(c.y,d.y)&&
	max(c.x,d.x) >= min(a.x,b.x)&&
	max(c.y,d.y) >= min(a.y,b.y)&&
    fcmd(cross(c-a,b-a)*cross(d-a,b-a)) <= 0 &&
    fcmd(cross(a-c,d-c)*cross(b-c,d-c)) <= 0;
}

Point lineinx(Point a,Point b,Point c,Point d)  // 求直线交点 
{
	Point u = a-c;
	double t = cross(d-c,u) / cross(b-a,d-c);
	Point tmp = b-a; 
	Point p = a + Point(tmp.x*t,tmp.y*t);
	return p;
}

Point p[5];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
    	for(int i = 0; i < 4; i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
        if(seginx(p[0],p[1] ,p[2],p[3]) == 0)  // 无交点 
        {
        	printf("0.00\n");
        	continue;
		}
		if(fcmd(cross(p[1]-p[0],p[3]-p[2])) == 0) // 平行或重合 
		{
			printf("0.00\n");
			continue;
		}
		if(fcmd(p[0].y-p[1].y) == 0 || fcmd(p[2].y-p[3].y) == 0) // 某线平着 
		{
			printf("0.00\n");
			continue;
		}
		if(fcmd(p[0].y - p[1].y) < 0) swap(p[0],p[1]);
		if(fcmd(p[2].y - p[3].y) < 0) swap(p[2],p[3]);
		
		if(seginx(p[0],Point(p[0].x,100000),p[2],p[3]))  // 入口遮住 
		{
			printf("0.00\n");
			continue;
		}
		
		if(seginx(p[2],Point(p[2].x,100000),p[0],p[1]))  // 入口遮住 
		{
			printf("0.00\n");
			continue;
		}
		
		Point pj = lineinx(p[0],p[1],p[2],p[3]);
		Point v1 = lineinx(p[0],p[1],p[2],Point(100000,p[2].y));
		Point v2 = lineinx(p[2],p[3],p[0],Point(100000,p[0].y));
		double s1 = fabs(cross(v1-pj,p[2]-pj))/2.0;
		double s2 = fabs(cross(v2-pj,p[0]-pj))/2.0;
		double ans = min(s1,s2);
		printf("%.2f\n",ans);
	}
	return 0; 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值