zoukankan      html  css  js  c++  java
  • POJ 3304 Segments

    判断直线与线段相交

    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<list>
    #include<algorithm>
    using namespace std;
    
    int T,n;
    int tot;
    struct point
    {
        double x;
        double y;
    
    } p[200+10];
    struct Line
    {
        point a;
        point b;
    } line[200+10];
    
    const double eps=1e-8;
    
    int intersect_in(point a,point b,point c,point d)
    {
        double X1=a.x;
        double Y1=a.y;
        double X2=b.x;
        double Y2=b.y;
    
        double A=Y2-Y1;
        double B=-(X2-X1);
        double C=Y2*(X2-X1)-X2*(Y2-Y1);
    
        if((A*c.x+B*c.y+C)*(A*d.x+B*d.y+C)<=eps) return 1;
        return 0;
    }
    
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            tot=0;
            for(int i=1; i<=n; i++)
            {
                double a,b,c,d;
                scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
                p[tot+0].x=a;
                p[tot+0].y=b;
                p[tot+1].x=c;
                p[tot+1].y=d;
                line[i].a=p[tot+0];
                line[i].b=p[tot+1];
                tot=tot+2;
            }
    
            if(n==1||n==2)
            {
                printf("Yes!
    ");
                continue;
            }
    
            int ans=0;
            for(int i=0; i<tot; i++)
            {
                for(int j=i+1; j<tot; j++)
                {
                    if(sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y))<eps) continue;
    
                    bool flag=0;
                    for(int k=1; k<=n; k++)
                    {
                        if(intersect_in(p[i],p[j],line[k].a,line[k].b)==0)
                        {
                            flag=1;//不相交
                           break;
                        }
                    }
                    if(flag==0)
                    {
                        ans=1;
                        break;
                    }
                }
                if(ans) break;
            }
            if(ans) printf("Yes!
    ");
            else printf("No!
    ");
        }
        return 0;
    }
  • 相关阅读:
    UI自动化之鼠标、键盘事件
    iframe框中元素定位
    接口 Interface
    序列化和反序列化
    密封类和部分类
    简单工场设计模式
    ADO.NET数据库操作
    集合
    里氏转换
    装箱和拆箱
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5153557.html
Copyright © 2011-2022 走看看