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
Output:
1.00
0.00
思路:
分好多情况讨论,代码倒是不难写
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 10010;
const double eps = 1e-8;
int sign(double x)
{
if (fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
int dcmp(double x, double y)
{
if (fabs(x - y) < eps) return 0;
return x > y ? 1 : -1;
}
struct Point
{
double x, y;
Point(double x = 0, double y = 0) :x(x), y(y) { };
Point operator + (Point t) { return Point(x + t.x, y + t.y); }
Point operator - (Point t) { return Point(x - t.x, y - t.y); }
Point operator * (double t) { return Point(x * t, y * t); }
Point operator / (double t) { return Point(x / t, y / t); }
void read() { cin >> x >> y; }
}P[N];
typedef Point Vector;
struct Line
{
Point p, q;
Vector v;
Line() { }
Line(Point p, Point q) :p(p), q(q) { v = q - p; }
}L[4];
double cross(Vector a, Vector b)
{
return a.x * b.y - a.y * b.x;
}
double cross(Point A, Point B, Point C)
{
return cross(B - A, C - A);
}
double dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y;
}
double length(Vector a)
{
return sqrt(dot(a, a));
}
bool OnSegment(Point p, Point a, Point b)
{
return sign(cross(a - p, b - p)) == 0 && sign(dot(a - p, b - p)) <= 0;
}
bool Segment_proper_intersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
double c3 = cross(b2 - b1, a1 - b1), c4 = cross(b2 - b1, a2 - b1);
if (!sign(c1) || !sign(c2) || !sign(c3) || !sign(c4))
{
bool f1 = OnSegment(b1, a1, a2);
bool f2 = OnSegment(b2, a1, a2);
bool f3 = OnSegment(a1, b1, b2);
bool f4 = OnSegment(a2, b1, b2);
bool f = (f1 | f2 | f3 | f4);
return f;
}
return (sign(c1) * sign(c2) < 0 && sign(c3) * sign(c4) < 0);
}
Point Get_line_intersection(Point p, Vector v, Point q, Vector w)
{
Vector u = p - q;
double t = cross(w, u) / cross(v, w);
Point b = v * t;
return p + b;
}
int main()
{
int T;
cin >> T;
while (T--)
{
Point a, b, c, d, o;
a.read(), b.read(), c.read(), d.read();
if (a.x > b.x) swap(a, b);
if (c.x > d.x) swap(c, d);
L[1] = Line(a, b), L[2] = Line(c, d);
if (!Segment_proper_intersection(L[1].p, L[1].q, L[2].p, L[2].q))
{
puts("0.00");
continue;
}
else o = Get_line_intersection(L[1].p, L[1].v, L[2].p, L[2].v);
if (L[1].v.y == 0 || L[2].v.y == 0)
{
puts("0.00");
continue;
}
int cnt = (L[1].p.y > o.y) + (L[1].q.y > o.y) + (L[2].p.y > o.y) + (L[2].q.y > o.y);
if (cnt < 2)
{
puts("0.00");
continue;
}
P[1] = L[1].p.y > o.y ? L[1].p : L[1].q;
P[2] = L[2].p.y > o.y ? L[2].p : L[2].q;
P[1] = P[1] - o; P[2] = P[2] - o;
if (P[1].x == 0|| P[2].x == 0)
{
if (P[2].x == 0) swap(P[1], P[2]);
double h, l, t, area;
if (P[1].y < P[2].y)
{
t = P[1].y / P[2].y;
Point dot = P[2] * t;
l = length(dot - P[1]);
h = P[1].y;
}
else
{
t = P[2].y / P[1].y;
Point dot = P[1] * t;
l = length(dot - P[2]);
h = P[2].y;
}
area = (l * h) / 2.0;
printf("%.2lf\n", fabs(area) + eps);
}
else
{
double a = atan2(P[1].y, P[1].x), b = atan2(P[2].y, P[2].x);
if (atan2(P[1].y, P[1].x) > atan2(P[2].y, P[2].x)) swap(P[1], P[2]);
if (P[1].x < 0)
{
P[1].x = -P[1].x, P[2].x = -P[2].x;
swap(P[1], P[2]);
}
if (P[2].y > P[1].y && P[2].x >= P[1].x && P[2].x > 0)
{
puts("0.00");
continue;
}
double h, l, t, area;
if (P[1].y > P[2].y)
{
t = P[2].y / P[1].y;
Point dot = P[1] * t;
l = length(dot - P[2]);
h = P[2].y;
}
else
{
t = P[1].y / P[2].y;
Point dot = P[2] * t;
l = length(dot - P[1]);
h = P[1].y;
}
printf("%.2lf\n", fabs((h * l) / 2.0) + eps);
}
}
return 0;
}