POJ 3304 Segments (computational geometry basis)

Source: Internet
Author: User

Segments

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11593 Accepted: 3657

Description

Given n segments in the dimensional space, write a program, which determines if there exists a line such that After projecting these segments on it, all projected segments has at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test Cases follow. Each test case begins with a line containing a positive integer n ≤100 showing the number of segments. After then, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) is the coordinates of the endpoints for one of the SEG ments.

Output

For each test case, the your program must output "yes!", if a line with the desired property exists and must output "no!" Otherwis E. You must assume, floating point numbers a and b is equal if | a - b| < 10-8.

Sample Input

321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1 .0

Sample Output

yes! yes! No!

Source

Amirkabir University of Technology Local Contest 2006

Ideas

If there is such a line, over the projection intersection area as the vertical line, the perpendicular to each segment must intersect, the question turns to ask whether there is a line and all segments intersect
If there is a line that intersects all the segment cameras, the line must pass through one of the two endpoints of those segments, so enumerating any two endpoints can

Code

1#include <cmath>2#include <cstdio>3#include <cstring>4 #definefor (A,B,C) for (int a= (b);a< (c); a++)5 using namespacestd;6 7 Const intN = -+Ten;8 Const DoubleEPS = 1e-8;9 Ten intDCMP (Doublex) { One     if(x<eps)return 0;Else returnx<0? -1:1; A } -  - structLine { the     DoubleX1,y1,x2,y2; - }l[n]; -  - intN; +  - DoubleCrossDoubleX1,DoubleY1,DoubleX2,DoubleY2,DoubleXDoubley) { +     return(x2-x1) * (y-y1)-(x-x1) * (y2-y1); A } at BOOLJudgeDoubleX1,DoubleY1,DoubleX2,Doubley2) { -     if(!DCMP (x2-x1) &&!dcmp (y2-y1))return 0; -for (I,0, N) { -         if(Cross (X1,Y1,X2,Y2,L[I].X1,L[I].Y1) * -Cross (x1,y1,x2,y2,l[i].x2,l[i].y2) >eps)return 0; -     } in     return 1; - } to  + intMain () { -     intT; thescanf"%d",&T); *      while(t--) { $scanf"%d",&n);Panax Notoginsengfor (I,0, N) -scanf"%LF%LF%LF%LF",&l[i].x1,&l[i].y1,&l[i].x2,&l[i].y2); the         if(n==1) {puts ("yes!");Continue; } +         BOOLans=0; Afor (I,0, N) { thefor (j,i+1, N) { +                 if(Judge (l[i].x1,l[i].y1,l[j].x1,l[j].y1)) ans=1; -                 if(Judge (L[i].x1,l[i].y1,l[j].x2,l[j].y2)) ans=1; $                 if(Judge (l[i].x2,l[i].y2,l[j].x1,l[j].y1)) ans=1; $                 if(Judge (L[i].x2,l[i].y2,l[j].x2,l[j].y2)) ans=1; -                 if(ANS) Break; -             } the             if(ANS) Break; -         }Wuyi         if(ANS) puts ("yes!"); the         ElsePuts"no!"); -     } Wu     return 0; -}

POJ 3304 Segments (computational geometry basis)

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.