这个Easy Problem确实挺Easy的,但就是细节很多,要考虑的主要有一下几点:
一、任一一条木板于x轴平行时,答案为0;
二、一条木板在另一条上方时,答案为0(水是垂直掉下来的,这种情况掉不到两块木板中间);
三、两木板没有交点时,答案为0;
四、有点的情况下,求相应的面积即可。
题目不难,但细节比较多,代码会比较长。计算几何很多时候都有这种特点吧!
/*
* Author: stormdpzh
* Created Time: 2013/3/29 13:32:50
* File Name: poj_2826.cpp
*/
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <functional>
#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define mset(a, b) memset(a, b, sizeof(a))
#define lint long long
using namespace std;
const int INF = 1 << 30;
const int MaxN = 100005;
const double eps = 1e-8;
int sgn(double d)
{
if(d > eps) return 1;
if(d < -eps) return -1;
return 0;
}
struct Point
{
double x, y;
Point () {}
Point(double _x, double _y) : x(_x), y(_y) {}
void read()
{
scanf("%lf%lf", &x, &y);
}
};
Point l1[2], l2[2];
Point operator - (const Point &p1, const Point &p2)
{
return Point(p1.x - p2.x, p1.y - p2.y);
}
double operator * (const Point &p1, const Point &p2)
{
return p1.x * p2.y - p1.y * p2.x;
}
bool get_segment_intersection(const Point &p1, const Point &p2, const Point &p3, const Point &p4, Point &c)
{
double d1 = (p2 - p1) * (p3 - p1);
double d2 = (p2 - p1) * (p4 - p1);
double d3 = (p4 - p3) * (p1 - p3);
double d4 = (p4 - p3) * (p2 - p3);
int s1 = sgn(d1), s2 = sgn(d2), s3 = sgn(d3), s4 = sgn(d4);
if(s1 * s2 > 0 || s3 * s4 > 0 || sgn(d1 - d2) == 0) return false;
c = Point((p3.x * d2 - p4.x * d1) / (d2 - d1), (p3.y * d2 - p4.y * d1) / (d2 - d1));
return true;
}
bool get_line_intersection(const Point &p1, const Point &p2, const Point &p3, const Point &p4, Point &c)
{
double d1 = (p2 - p1) * (p3 - p1);
double d2 = (p2 - p1) * (p4 - p1);
if(0 == sgn(d1 - d2)) return false;
c = Point((p3.x * d2 - p4.x * d1) / (d2 - d1), (p3.y * d2 - p4.y * d1) / (d2 - d1));
return true;
}
bool is_parallel_to_x_axis(const Point p1, const Point p2)
{
if(sgn(p1.y - p2.y) == 0) return true;
return false;
}
double gao(Point cr)
{
Point l3[2];
double x1, y1, x2, y2;
Point seg1[2], seg2[2];
Point tmp;
seg1[0] = seg2[0] = cr;
if(sgn(l1[0].y - l1[1].y) > 0) {
x1 = l1[0].x;
y1 = l1[0].y;
}
else {
x1 = l1[1].x;
y1 = l1[1].y;
}
if(sgn(l2[0].y - l2[1].y) > 0) {
x2 = l2[0].x;
y2 = l2[0].y;
}
else {
x2 = l2[1].x;
y2 = l2[1].y;
}
if(sgn(y1 - y2) < 0) {
if(sgn(l1[0].x - l1[1].x) != 0) {
int s = sgn((l1[0].y - l1[1].y) / (l1[0].x - l1[1].x));
bool f = get_line_intersection(l2[0], l2[1], Point(x1, y1), Point(x1, y1 + 1.0), tmp);
if(f && sgn(tmp.y - y1) >= 0) {
if(s > 0) {
if(sgn(x2 - tmp.x) >= 0) return 0.0;
}
if(s < 0) {
if(sgn(x2 - tmp.x) <= 0) return 0.0;
}
}
}
l3[0] = Point(x1, y1);
l3[1] = Point(x1 + 1.0, y1);
get_line_intersection(l3[0], l3[1], l2[0], l2[1], tmp);
}
else {
if(sgn(l2[0].x - l2[1].x) != 0) {
int s = sgn((l2[0].y - l2[1].y) / (l2[0].x - l2[1].x));
bool f = get_line_intersection(l1[0], l1[1], Point(x2, y2), Point(x2, y2 + 1.0), tmp);
if(f && sgn(tmp.y - y2) >= 0) {
if(s > 0) {
if(sgn(x1 - tmp.x) >= 0) return 0.0;
}
if(s < 0) {
if(sgn(x1 - tmp.x) <= 0) return 0.0;
}
}
}
l3[0] = Point(x2, y2);
l3[1] = Point(x2 + 1.0, y2);
get_line_intersection(l3[0], l3[1], l1[0], l1[1], tmp);
}
seg1[1] = l3[0];
seg2[1] = tmp;
return fabs((seg1[1] - seg1[0]) * (seg2[1] - seg2[0])) / 2.0;
}
int main()
{
int t;
Point cross_p;
scanf("%d", &t);
while(t--) {
l1[0].read();
l1[1].read();
l2[0].read();
l2[1].read();
if(is_parallel_to_x_axis(l1[0], l1[1]) || is_parallel_to_x_axis(l2[0], l2[1])) {
puts("0.00");
continue ;
}
bool f = get_segment_intersection(l1[0], l1[1], l2[0], l2[1], cross_p);
if(!f) {
puts("0.00");
continue ;
}
printf("%.2lf\n", gao(cross_p));
}
return 0;
}