This is a detailed question. In the question, let's use two lines to connect the rain. The rain drops vertically and ask how much water we can connect with the given two lines. We will discuss it in many cases here. 1: two line segments do not want to be handed over when there is no rain 2: two line segments overlap when there is no rain 3: the intersection of the two line segments is the same as the highest point of a line segment in the middle, unable to receive rain 4: there is a line segment that does not normally receive rain. 5: when the highest points of the two line segments are on the intersection side, the higher points in the period cover the lower points and the rain cannot be caught. Note: It is very important to obtain the point when calculating the area at last. My program has annotations. I have contributed 5 or 6 times in this area to find out. After considering various situations that cannot be received, I accidentally changed a place ..... Depressed .. An easy problem ?!
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:6739 |
|
Accepted:969 |
Description
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
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
20 1 1 01 0 2 10 1 2 11 0 1 2
Sample output
1.000.00
# Include <iostream> # include <algorithm> # include <cmath> # include <cstdio> # include <complex> # include <cstring> # include <string> # include <iomanip> using namespace STD; # define max (A, B) (A> B? A: B) # define min (a, B) (a <B? A: B) # define EPS 1e-8 # define zero (x)> 0? (X):-(x) <EPS) struct point {Double X, Y ;}; struct line {point A, B ;}; int N; line L1, L2, l3; double trianglearea (point a, point B, point C) // calculates the area {Double K =. x * B. Y + B. x * C. Y + C. x *. y-b.x *. y-c.x * B. y-a.x * C. y; return K> 0? K/2:-k/2;} int sig (Double K) {If (FABS (k) <EPS) return 0; Return (k> 0 )? 1:-1;} double det (double X1, double Y1, double X2, double Y2) {return X1 * Y2-X2 * Y1;} double xmult (point P0, point P1, point P2) {return det (p1.x-release X, p1.y-release y, p2.x-release X, p2.y-release y);} double dotdet (double X1, double Y1, double X2, double Y2) {return X1 * X2 + Y1 * Y2;} double dot (point P0, Point P1, point P2) {return dotdet (p1.x-forwarded X, p1.y-forwarded y, p2.x-0000x, p2.y-0000y);} int between (point P0, Point P1, Point P2) {return sig (dot (P0, P1, P2);} int intersect1 (point a, point B, point C, point D) // determine the intersection of line segments {double S1, S2, S3, S4; int d1 = sig (S1 = xmult (a, B, c )); int D2 = sig (s2 = xmult (A, B, D); int D3 = sig (S3 = xmult (c, d, )); int D4 = sig (S4 = xmult (c, d, B); If (d1 ^ D2) =-2 & (D3 ^ D4) =-2) {return 1 ;}if (d1 = 0 & between (C, A, B) <= 0) return 2; If (d2 = 0 & between (D, a, B) <= 0) return 2; If (D3 = 0 & between (a, c, d) <= 0) return 2; If (D4 = 0 & between (B, c, d) <= 0) return 2; return 0;} bool parallel (const line & U, const line & V) // determine whether two straight lines are parallel x1y2-y1x2 = 0 {return zero (U. a. x-u. B .x) * (v. a. y-v. B .y)-(U. a. y-u. B .y) * (v. a. x-v. B .x);} point intersection (line & U, line & V) // calculates the intersection of two straight lines {point temp = u. a; Double T = (U. a. x-v.a.x) * (v. a. y-v. B .y)-(U. a. y-v.a.y) * (v. a. x-v. B .x)/(U. a. x-u. B. x) * (v. a. y-v. B .y)-(U. a. y-u. B .y) * (v. a. x-v. B .x); temp. X + = (U. b. x-u.a.x) * t; temp. Y + = (U. b. y-u.a.y) * t; return temp;} bool hide (line U, line V, point in) // determines if there is an entrance covered by {point temp1, temp2; temp1 = (U. a. y> = v. a. y? U. A: V. A); temp2 = (U. A. Y> = V. A. Y? V. a: U. a); If (temp1.x <= temp2.x & temp2.x <in. X & xmult (in, temp1, temp2)> EPS) return true; If (temp1.x> = temp2.x & temp2.x> in. X & xmult (in, temp1, temp2) <EPS) return true; return false;} void Init () {scanf ("% lf ", & l1.a. x, & l1.a. y, & l1. B. x, & l1. B. y); scanf ("% lf", & l2.a. x, & l2.a. y, & l2. B. x, & l2. B. y); If (l1.a. Y <l1. B. y) Swap (l1.a, l1. B); If (l2.a. Y <l2. B. y) Swap (l2.a, l2. B);} double solve () {line LL; ll. a. X = ll. a. y = 0; Ll. b. X = 1; ll. b. y = 0; point temp, temp2; If (parallel (L1, L2) // two lines are parallel and return 0.00 is coincident; If (parallel (LL, L2) | parallel (LL, L1) // horizontal return 0.00 of a line; If (intersect1 (l1.a, l1. B, l2.a, l2. B )! = 1 & intersect1 (l1.a, l1. B, l2.a, l2. B )! = 2) // do not want to submit the return 0.00; temp = (l1.a. Y <l2.a. Y? L1.a: l2.a); l3. B = temp; temp. x-= 10000; l3.a = temp; temp = intersection (L1, L2); // The intersection is as high as the lower point, is the high point of the parallel and intersection of a board if (temp. y> = l1.a. Y | temp. y> = l2.a. y) return 0.00; If (hide (L1, L2, temp) // The higher the value cannot cover the lower point return 0.00; If (l1.a. y> = l2.a. y) // select a vertex. Do not select an incorrect vertex because the lowest point selected when the two points are equal to Y is the right vertex. Therefore, the intersection of the calculated horizontal line must be the left vertex. Temp2 = intersection (L1, L3); elsetemp2 = intersection (L2, L3); Return trianglearea (l3. B, temp, temp2);} int main () {CIN> N; while (n --) {Init (); cout <setprecision (2) <setiosflags (IOs: fixed) <solve () <Endl;} return 0 ;}