Poj 3304 segments (whether a straight line and a line segment InterSect)

Source: Internet
Author: User

Question Link

Can you find a straight line so that all the given line segments have a common point on the projection of this line.

Idea: assume that there is a line a so that all the line segments have a common point in the projection of the line, there must be a line B perpendicular to line A, and line B must intersection with all the line segments, therefore, the problem is whether a line exists and all the line segments are in intersection.

If such a straight line exists, the line may overlap with the endpoint of one or some line segments, or it may not overlap. For line segments that do not have intersection at the endpoint, we can rotate or translate this line, let it first intersect with the endpoint of the online segment of a line segment (then this line and other line segments are in the middle of other line segments), and then continue to rotate, let this line and other lines also intersection at the endpoint. So far, we can understand that all such straight lines can be translated and rotated to process them. In turn, if a straight line composed of the endpoints of all line segments (any combination) does not meet the requirement of having at least one common point with all line segments, that is, such a straight line does not exist.

It should be noted that smaller than 1e-8 is the key point, so pay attention to the Judgment

 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 #define eps 1e-8 7  8 using namespace std ; 9 10 struct point11 {12     double x,y;13 }p[210];14 struct line15 {16     point a,b;17 }L[110];18 19 double multi(point a,point b,point c)20 {21     return ((a.x-c.x)*(b.y-c.y) - (b.x-c.x)*(a.y-c.y)) ;22 }23 bool inter(line L,point a,point b)24 {25     double x1 = multi(L.a,a,b) ;26     double x2 = multi(L.b,a,b) ;27     if((x1 > eps && x2 < -eps) || (x1 < -eps && x2 > eps) || (fabs(x1) < eps) || (fabs(x2) < eps))28         return true ;29     return false ;30 }31 int main()32 {33     int T ,n;34     scanf("%d",&T) ;35     while(T--)36     {37         scanf("%d",&n) ;38         int cnt = 0 ;39         for(int i = 0 ; i < n ; i++)40         {41             scanf("%lf %lf %lf %lf",&L[i].a.x,&L[i].a.y,&L[i].b.x,&L[i].b.y) ;42             p[cnt ++] = L[i].a ;43             p[cnt ++] = L[i].b ;44         //    printf("2\n") ;45         }46         //printf("1\n") ;47         bool ans = false ;48         for(int i = 0 ; i < cnt-1 ; i++)49         {50             for(int j = i+1 ; j < cnt ; j++)51             {52                 if(fabs(p[i].x-p[j].x) < eps && fabs(p[i].y-p[j].y) < eps) continue ;53                 bool flag = true ;54                 for(int k = 0 ; k < n ; k++)55                 {56                     if(!inter(L[k],p[i],p[j]))57                     {58                         flag = false ;59                         break ;60                     }61                 }62                 if(flag)63                 {64                     ans = true ;65                     break ;66                 }67             }68         }69         if(ans) puts("Yes!") ;70         else puts("No!") ;71     }72     return 0 ;73 }
View code

 

Poj 3304 segments (whether a straight line and a line segment InterSect)

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.