Calculate the geometric poj 2826 an easy problem ?! (Determine the line segment position and calculate the intersection point)

Source: Internet
Author: User

This question is so sad. I have been wa for a long time because I have misunderstood the question.

Let's talk about how to solve the problem first:

1. When the two line segments do not want to be handed over, the result is 0.00. Parallel conditions are excluded here;

2. When either of the two line segments is parallel to the X axis, the result is also 0.00;

3. When the two line segments intersect but are in the same line (determined in parallel), the result is also 0.00;

4. Another situation is that, if one of the above boards blocks the following boards, the same result would be 0.00;

In addition to the above four cases, we can use the cross point obtained by the cross product, and then obtain the Y value of the horizontal plane that can be filled with water above the intersection, and use the cross product to calculate the area.

This question has been wa for a long time because I think that when two boards don't want to be handed in, I can use the ground as a bottom, and then ask for the area of the trapezoid. As a result, WA has been a long time, so sad ......

The following code is for reference only ......

# Include <cstring> # include <cstdlib> # include <cstdio> # include <iostream> # include <cmath> using namespace STD; const double EPS = 1e-8; int dblcmp (Double X) {If (FABS (x) <EPS) return 0; return x> 0.0? 1:-1;} typedef struct point {Double X, Y; point () {} Point (double Tx, double ty) {x = Tx, y = ty ;}} point; double dotmult (point a, point B, point C) {return (C. x-a.x) * (B. x-c.x) + (C. y-a.y) * (B. y-c.y);} double xmult (point P1, point P2, point P0) {return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p1.y-Snapshot y) * (p2.x-0000x);} int iscross (point a, point B, point C, point D) // The normalized intersection {int T1 = dblcmp (xmult (C,, b); int t2 = dblcmp (xmult (D, a, B); int T3 = Dblcmp (xmult (A, C, D); int t4 = dblcmp (xmult (B, c, d )); if (T1 * t2 <0 & T3 * t4 <0) return 1; return 0;} int iscross2 (point a, point B, point C, point D) // non-normalized intersection {int T1 = dblcmp (xmult (C, A, B); int t2 = dblcmp (xmult (D, a, B )); int T3 = dblcmp (xmult (A, C, D); int t4 = dblcmp (xmult (B, c, d )); if (T1 * t2 <0 & T3 * t4 <0) return 1; if (T1 = 0 & dblcmp (dotmult (a, B, c)> = 0) return 1; if (t2 = 0 & dblcmp (dotmult (a, B, d)> = 0) return 1; if (t3 = 0 & DB Lcmp (dotmult (c, d, A)> = 0) return 1; if (t4 = 0 & dblcmp (dotmult (c, d, B)> = 0) return 1; return 0;} Point getcrosspoint (point a, point B, double YY) {double dx = B. x-a.x; double DY = B. y-a.y; If (dblcmp (dx) = 0) {return point (. x, YY);} else {Double K = Dy/dx; If (dblcmp (K) = 0) {return point (max (. x, B. x), YY);} else {return point (yy-a.y)/K +. x, YY) ;}}int isupon (point, point); double solvetringle (Point P1, point P2, point P3, point P4) {If (dblcmp (p2.x-p1.x) * (p4.y-p3.y)-(p2.y-p1.y) * (p4.x-p3.x) = 0) return 0.0; If (isupon (P1, P2, P3, P4) return 0.00; double T1 = xmult (P1, P3, p4); double t2 = xmult (P2, P3, P4); point P0; struct x = (T1 * p2.x-T2 * p1.x)/(t1-t2 ); p0.y = (T1 * p2.y-T2 * p1.y)/(t1-t2); double YY1 = max (p1.y, p2.y); double yy2 = max (p3.y, p4.y); double YY; if (dblcmp (yy1-p0.y) = 0 | dblcmp (yy2-p0.y) = 0) return 0.00; If (dblcmp (yy1-yy2)> = 0) {YY = yy2; point P11 = getcrosspoint (P1, P2, YY); point P22 = getcrosspoint (P3, P4, YY ); return ABS (xmult (P11, P22, P0)/2.0;} else {YY = YY1; point P11 = getcrosspoint (P1, P2, YY ); point P22 = getcrosspoint (P3, P4, YY); Return ABS (xmult (P11, P22, P0)/2.0 ;}} void myswap (point & A, point & B) {point t; T. X =. x, t. y =. y;. X = B. x,. y = B. y; B. X = T. x, B. y = T. y;} int isupon (point P1, point P2, point P3, point P 4) // determine whether to block ...... {If (iscross2 (P1, P2, P4, point (p4.x, 10000.0) return 1; return 0 ;}int main () {// freopen ("poj2826in.txt ", "r", stdin); // freopen ("myout.txt", "W", stdout); int t; double ans; Point P1, P2, P3, P4; scanf ("% d", & T); While (t --) {scanf ("% lf", & p1.x, & p1.y, & p2.x, & p2.y); scanf ("% lf", & p3.x, & p3.y, & p4.x, & p4.y); If (dblcmp (p1.y-p2.y)> 0) myswap (P1, P2); If (dblcmp (p3.y-p4.y)> 0) myswap (P3, P4); If (dblcmp (p2.y-p4.y) <0) {myswap (P1, P3); myswap (P2, P4);} If (dblcmp (p2.y-p1.y) = 0) ans = 0.00; else if (dblcmp (p4.y-p3.y) = 0) ans = 0.00; else if (iscross2 (P1, P2, P3, P4) ans = solvetringle (P1, P2, P2, p3, P4); else ans = 0.00; printf ("%. 2f \ n ", ANS);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.