Hdu 1086 (judging segment intersection)

Source: Internet
Author: User

Portal: You can Solve a Geometry problem too

Test instructions: For n line segments, determine the number of points to intersect.

Analysis: To determine the intersection template problem, rapid rejection of the experimental principle is that each segment represents the vector and one end of the line segment and the other segment of the two end of the two vectors of the cross product, if the line intersection then the other line segment two endpoints must be on both sides of the segment, Then this segment represents the vector must be clockwise to turn the counter-clockwise, the cross product must be less than or equal to 0, the same to another line segment such a judgment can be.

#include <algorithm>#include<cstdio>#include<cstring>#include<cmath>#defineN 1010#definePI ACOs (-1.0)using namespacestd;Const Doubleeps=1e-8;intSgnDoublex) {    if(Fabs (x) <eps)return 0; if(x<0)return-1; return 1;}structpoint{Doublex, y; Point () {}, point (Double_x,Double_y): X (_x), Y (_y) {} pointoperator-(ConstPoint &a)Const    {        returnPoint (x-a.x,y-a.y); }    Double operator*(ConstPoint &a)Const    {        returna.x*x+a.y*y; }    Double operator^(ConstPoint &a)Const    {        returnx*a.y-y*a.x; }};structline{Point S,e; Line () {} line (point _s,point _e): S (_s), E (_e) {}};BOOLsegcrossseg (line l1,line L2) {returnMax (l1.s.x,l1.e.x)>=min (l2.s.x,l2.e.x) &&Max (l2.s.x,l2.e.x)>=min (l1.s.x,l1.e.x) &&Max (L1.S.Y,L1.E.Y)>=min (L2.S.Y,L2.E.Y) &&Max (L2.S.Y,L2.E.Y)>=min (L1.S.Y,L1.E.Y) &&SGN (L1.s-L1.E) ^ (L2.S-L1.E)) *SGN ((l1.s-l1.e) ^ (L2.E-L1.E)) <=0&&SGN (L2.s-L2.E) ^ (L1.S-L2.E)) *SGN ((l2.s-l2.e) ^ (L1.E-L2.E)) <=0;} Line l[ the];intMain () {intN;  while(SCANF ("%d",&N), N) { for(intI=1; i<=n;i++)        {            Doublea,b,c,d; scanf ("%LF%LF%LF%LF",&a,&b,&c,&d); L[i]=Line (point (b), point (C,d)); }        intans=0;  for(intI=1; i<=n;i++)             for(intj=i+1; j<=n;j++)            if(Segcrossseg (L[i],l[j])) ans++; printf ("%d\n", ans); }}
View Code

Hdu 1086 (judging segment intersection)

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.