POJ 3304 Segments 枚举+叉积判断直线和线段是否相交

http://poj.org/problem?id=3304

Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have 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 that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

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!

题目大意:给出n条线段,问能否找到一条直线,使得所有线段在该条直线上的投影至少有一个交点。

思路:若存在这样的一条直线,那么就存在一条这条直线的垂线使得每条线段都跟这个垂线有交点。问题就转换成了能不能找到与所有线段都有交点的直线,枚举所有的端点看能不能找到满足题意的直线就行了。你可能会对这个枚举操作有点疑惑,因为这个直线不一定过给出的线段的顶点啊。确实如此,但是我们可以对这条直线进行旋转操作,必定能得到一条满足题意的且经过给出的线段顶点的直线。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;

const double pi=acos(-1.0);//弧度pi
const double eps=1e-8;//精度

struct point
{
    double x,y;
    point(double a=0,double b=0)
    {
        x=a,y=b;
    }
    friend point operator * (point a,double b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (double a,point b)
    {
        return point(b.x*a,b.y*a);
    }
    point operator - (const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    point operator + (const point &b)const
    {
        return point(x+b.x,y+b.y);
    }
    point operator / (const double b)const
    {
        return point(x/b,y/b);
    }
    bool operator < (const point &b)const//按坐标排序
    {
        if(fabs(x-b.x)<eps)
            return y<b.y-eps;
        return x<b.x-eps;
    }
    void transxy(double sinb,double cosb)//逆时针旋转b弧度
    {                                      //若顺时针 在传入的sinb前加个-即可
        double tx=x,ty=y;
        x=tx*cosb-ty*sinb;
        y=tx*sinb+ty*cosb;
    }
    void transxy(double b)//逆时针旋转b弧度
    {                     //若顺时针传入-b即可
        double tx=x,ty=y;
        x=tx*cos(b)-ty*sin(b);
        y=tx*sin(b)+ty*cos(b);
    }
    double norm()
    {
        return sqrt(x*x+y*y);
    }
};

inline double dot(point a,point b)//点积
{
    return a.x*b.x+a.y*b.y;
}
inline double cross(point a,point b)//叉积
{
    return a.x*b.y-a.y*b.x;
}

inline double dist(point a,point b)//两点间距离
{
    return (a-b).norm();
}

inline int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    return -1;
}

typedef point Vector;

struct line
{
    point s,e;
    line(){}
    line(point _s,point _e)
    {
        s=_s,e=_e;
    }
};

double xmult(point p1,point p2,point p3)//p1p2 X p1p3
{
    return cross(p2-p1,p3-p1);
}

bool Seg_inter_line(line l1,line l2)//判断直线l1 与 线段l2 是否相交
{
    return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e))<=0;
}

int n;
line Line[105];

bool check(line l)
{
    if(sgn(dist(l.s,l.e))==0)//同一点
        return 0;
    for(int i=0;i<n;i++)
        if(Seg_inter_line(l,Line[i])==0)//没有交点
            return 0;
    return 1;
}

int main()
{
    int t;
    double x1,y1,x2,y2;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            Line[i]=line(point(x1,y1),point(x2,y2));
        }
        bool flag=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(check(line(Line[i].s,Line[j].s))||check(line(Line[i].s,Line[j].e))||
                   check(line(Line[i].e,Line[j].s))||check(line(Line[i].e,Line[j].e)))
                    flag=1;
                if(flag)
                    break;
            }
            if(flag)
                break;
        }
        printf("%s\n",flag==1?"Yes!":"No!");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值