Poj 2653 Pick-up sticks -- intersection of line segments -- vector

Source: Internet
Author: User

[Cpp]
/*
Question: Then I threw some sticks and output the numbers of the sticks (no sticks pressed) at the top.
 
Line Segment Intersection
 
Vector is used in the ontology.
Note the usage of v. erase (remove_if (v. begin (), v. end (), OK), v. end ();
At the same time, v. erase returns the location of the next element after deletion. Do not use it ++ to operate the next element in the loop.
*/
# Include <stdio. h>
# Include <vector>
# Include <algorithm>
Using namespace std;
Const double esp = 1e-8;
Struct point
{
Double x, y;
};
Struct node
{
Point p1, p2;
Int no;
};
Vector <node> v;
Int n;
Node cc;
Double max (double a, double B) {return a> B? A: B ;}
Double min (double a, double B) {return a <B? A: B ;}
Double cross (point p, point s, point e)
{
Return (e. x-s.x) * (p. y-s.y)-(p. x-s.x) * (e. y-s.y );
}
Bool segintersect (point s1, point e1, point s2, point e2)
{
Return (max (s1.x, e1.x)> = min (s2.x, e2.x ))&&
(Max (s2.x, e2.x)> = min (s1.x, e1.x ))&&
(Max (s1.y, e1.y)> = min (s2.y, e2.y ))&&
(Max (s2.y, e2.y)> = min (s1.y, e1.y ))&&
(Cross (s1, s2, e2) * cross (e1, s2, e2) <= esp )&&
(Cross (s2, s1, e1) * cross (e2, s1, e1) <= esp ));
}
Bool OK (node n)
{
If (segintersect (n. p1, n. p2, cc. p1, cc. p2 ))
Return true;
Return false;
}
Int main ()
{
Int I;
While (scanf ("% d", & n), n)
{
V. erase (v. begin (), v. end ());
For (I = 1; I <= n; I ++)
{
Scanf ("% lf", & cc. p1.x, & cc. p1.y, & cc. p2.x, & cc. p2.y );
Cc. no = I;
V. erase (remove_if (v. begin (), v. end (), OK), v. end ());
V. push_back (cc );
}
Printf ("Top sticks :");
For (vector <node >:: iterator it = v. begin (); it! = V. end (); it ++)
{
Printf ("% d", it-> no );
If (it! = V. end ()-1)
Printf (",");
Else printf (". \ n ");
}
} Www.2cto.com
Return 0;
}

Author: qq172108805

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.