码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3304 Segments

时间:2016-01-23 18:18:44      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

判断直线与线段相交

#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!\n");
            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!\n");
        else printf("No!\n");
    }
    return 0;
}

 

POJ 3304 Segments

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5153557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!