An Easy Problem?! POJ - 2826 (Geometry)

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), ( x  
                                                          4 , 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

There are a lot of pits, I don't want to say more. . . .

#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) // tristate function
{
	if(fabs(x) < 1e-12) return 0;
	return x > 0 ? 1 : -1;
}

bool seginx(Point a, Point b, Point c, Point d) // Determine the intersection of line segments
{
	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) // find the intersection of the lines
{
	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];

intmain()
{
    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) // no intersection
        {
        	printf("0.00\n");
        	continue;
		}
		if(fcmd(cross(p[1]-p[0],p[3]-p[2])) == 0) // parallel or coincident
		{
			printf("0.00\n");
			continue;
		}
		if(fcmd(p[0].yp[1].y) == 0 || fcmd(p[2].yp[3].y) == 0) // a line is flat
		{
			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])) // Entry blocked
		{
			printf("0.00\n");
			continue;
		}
		
		if(seginx(p[2],Point(p[2].x,100000),p[0],p[1])) // Entry is blocked
		{
			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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812891&siteId=291194637