POJ 3304 Segments(计算几何)

Description
给出n条线段,问是否存在一条直线与这n条线段的任一条都相交
Input
第一行一整数T表示用例组数,每组用例首先输入一整数n表示线段数量,之后n行每行四个实数x1,y1,x2,y2分别表示线段两端点横纵坐标
Output
对于每组用例,如果存在一条直线与这n条线段的任一条都相交则输出YES!,否则输出NO!
Sample Input
3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0
Sample Output
Yes!
Yes!
No!
Solution
如果存在一条合法直线与任一条线段都相交,那么通过适当的旋转一定能让这条直线经过2n个端点中的某两个,所以只需要C(2n,2)的枚举两个端点判断过这两点的直线是否合法即可
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111
#define eps 1e-8
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s=_s;e=_e;
    }
};
double dis(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//判断x的符号 
int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    return 1;
}
//判断线段相交
bool inter(Line l1,Line l2)
{
    return 
        sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s))<=0;
        //&&
       // sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s))<=0;
}
int T,n;
Point p[2*maxn];
Line l[maxn];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&p[2*i-1].x,&p[2*i-1].y,&p[2*i].x,&p[2*i].y);
            l[i]=Line(p[2*i],p[2*i-1]);
        }
        int flag=0;
        for(int i=1;i<=2*n&&!flag;i++)
            for(int j=i+1;j<=2*n&&!flag;j++)
            {
                if(dis(p[i],p[j])<eps)continue;
                Line t=Line(p[i],p[j]);
                flag=1;
                for(int k=1;k<=n;k++)
                    if(!inter(t,l[k]))
                    {
                        flag=0;
                        break;
                    }
                if(flag)break;
            }
        printf("%s\n",flag?"Yes!":"No!");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值