poj3304 Segments(计算几何+直线相交)

poj3304

题目

给你许多的线段,问存不存在一条直线使得所有线段在直线上的映射有至少一个交点。

思路

http://blog.sina.com.cn/s/blog_6635898a0100n2lv.html
要想到枚举两两端点,注意题目说的重点的存在。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

const int maxn=110;
const double eps = 1e-8;

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

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.x + y*b.y;
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
};

struct Line
{
    Point s,e;
    Line() {}
    Line(Point _s,Point _e)
    {
        s = _s;
        e = _e;
    }
};

double xmult(Point p0,Point p1,Point p2)
{
    return (p1-p0)^(p2-p0);
}

bool Seg_inter_line(Line l1,Line l2)
{
    return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= 0;
}

double dist(Point a,Point b)
{
    return sqrt( (b - a)*(b - a) );
}

Line line[maxn];

bool check(Line l1,int n)
{
    if(sgn(dist(l1.s,l1.e)) == 0 )return false;
    for(int i = 0; i < n; i++)
        if(Seg_inter_line(l1,line[i]) == false)
            return false;
    return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {

        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            double a,b,c,d;
            scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
            line[i]=Line(Point(a,b),Point(c,d));
        }
        int 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),n)||check(Line(line[i].s,line[j].e),n)||
                        check(Line(line[i].e,line[j].s),n)||check(Line(line[i].e,line[j].e),n))
                    flag=1;
                if(flag)
                    break;
            }
        if(flag)
            printf("Yes!\n");
        else
            printf("No!\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值