POJ 2826 An Easy Problem ?! Good question

Source: Internet
Author: User

POJ 2826 An Easy Problem ?! Good question

The general idea is that two blocks form a slot, asking how much rain can be installed in the slot, and paying attention to the vertical falling of rain, the idea is also very simple, that is, classification discussion is a bit bad.
1. If the two line segments do not overlap or are parallel, set 0;
2. There is a parallel X axis with 0 loaded;
3. If it overwrites the following, install 0;
4. For others, cross product is used to calculate the area.

Directly run the Code:

# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const double eps = 1e-8; struct point {double x; double y ;}; struct line {point a; point B ;}l1, l2; double ans; // calculate the Cross Product double xmult (point p0, point p1, point p2) {return (p1.x-converted X) * (p2.y-converted y)-(p2.x-converted X) * (p1.y-policy);} int dblcmp (double n) {if (fabs (n)
     
      
0? 1:-1;} // determines whether or not the intersection exists. How to intersection int judge (line l1, line l2) {double d1 = dblcmp (max (l1.a. x, l1. B. x)-min (l2.a. x, l2. B. x); double d2 = dblcmp (max (l2.a. x, l2. B. x)-min (l1.a. x, l1. B. x); double d3 = dblcmp (max (l1.a. y, l1. B. y)-min (l2.a. y, l2. B. y); double d4 = dblcmp (max (l2.a. y, l2. B. y)-min (l1.a. y, l1. B. y); double d5 = dblcmp (xmult (l2.a, l1.a, l1. B); double d6 = dblcmp (xmult (l2. B, l1.a, l1. B )); double d7 = dblcmp (xmult (l1.a, l2.a, l2. B); double d8 = dblcmp (xmult (l1. B, l2.a, l2. B )); if (d1> = 0 & d2> = 0 & d3> = 0 & d4> = 0) {if (d5 * d6> 0 | d7 * d8> 0) return 0; // non-Intersecting else if (d5 = 0 & d6 = 0) return 1; // if (d5 = 0 | d6 = 0 | d7 = 0 | d8 = 0) return 2; // endpoint intersection else return 3; // normalized intersection} return 0;} // returns the slope bool getslope (line l, double & k) {double t = l. a. x-l. B .x; if (t = 0) return false; k = (l. a. y-l. B .y)/t; return true;} // calculates the intersection of line segments point getIntersect (line l1, line l2) {point p; double A1 = l1. B. y-l1.a. y; double B1 = l1.a. x-l1. B. x; double C1 = (l1. B. x-l1.a. x) * l1.a. y-(l1. B. y-l1.a. y) * l1.a. x; double A2 = l2. B. y-l2.a. y; double B2 = l2.a. x-l2. B. x; double C2 = (l2. B. x-l2.a. x) * l2.a. y-(l2. B. y-l2.a. y) * l2.a. x; p. x = (C2 * B1-C1 * B2)/(A1 * B2-A2 * B1); p. y = (C1 * A2-C2 * A1)/(A1 * B2-A2 * B1); if (p. x =-0) p. x = 0; return p;} // calculate the point of point getbiggerY (point a, point B) {point q; if (dblcmp (. y-b.y)> 0) {q. x =. x; q. y =. y;} else {q. x = B. x; q. y = B. y;} return q;} double getarea (point p, point p1, point p2) {point q; double a; if (dblcmp (p1.y-p2.y)> = 0) {q. y = p2.y; q. x = p. x + (p1.x-p. x) * (p2.y-p. y)/(p1.y-p. y); // obtain the coordinate a = fabs (xmult (p, p2, q)/2; // calculate the area} else {q. y = p1.y; q. x = p. x + (p2.x-p. x) * (p1.y-p. y)/(p2.y-p. y); a = fabs (xmult (p, p1, q)/2;} return a;} int main () {int t; cin> t; while (t --) {point p1, p2, p; cin> l1.a. x> l1.a. y> l1. B. x> l1. B. y; cin> l2.a. x> l2.a. y> l2. B. x> l2. B. y; if (judge (l1, l2) <= 1) // determines whether the intersection exists {ans = 0; cout <0 <
      
        0) {// when the slope symbols of the two line segments are the same, int d1 = dblcmp (K1-K2); int d2 = dblcmp (k2 ); if (d1> 0 & d2> 0 & dblcmp (p2.x-p1.x) * dblcmp (p2.x-p. x) <= 0 | d1 <0 & d2> 0 & dblcmp (p1.x-p2.x) * dblcmp (p1.x-p. x) <= 0 | d1> 0 & d2 <0 & dblcmp (p1.x-p2.x) * dblcmp (p1.x-p. x) <= 0 | d1 <0 & d2 <0 & dblcmp (p2.x-p1.x) * dblcmp (p2.x-p. x) <= 0) // coverage // {ans = 0; // cout <3 <
       
      
     
    
   
  

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.