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; 
}

猜你喜欢

转载自blog.csdn.net/sunmoonvocano/article/details/79625796