【计算几何】 poj3304 Segments

Segments

Description

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 (x1y1) and (x2y2) 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条线段,问是否存在一条直线,所有线段在直线上的投影有公共点,即问是否存在一条直线,与所有线段都有交点。

要注意如果两个端点很近,则视为一个点,不能当作直线,以及N=1要特殊处理。


//判断线段和直线是否相交。
#include<cstdio>
#include<cstring>
using namespace std;
#define inf 1e-8
#define abs(x) ((x>0)?x:-(x))
struct point
{
    double x,y;
    point(){};
    point(double a,double b){x=a;y=b;};
};
struct line
{
    point s,e;
    line(){};
    line(point a,point b):s(a),e(b){};
}seg[105];
int n;
double cross(point a,point b,point o)
{
    return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
bool check(point a,point b)
{
    if(abs(a.x-b.x)<inf&&abs(a.y-b.y)<inf) return false;
    for(int i=0;i<n;++i)
    {
        if(cross(a,b,seg[i].s)*cross(a,b,seg[i].e)>inf)
           return false;
    }
    return true;
}
int main()
{
    int t;
    double x1,y1,x2,y2;
    scanf("%d",&t);
    for(bool flag=false;t--;flag=false)
    {
        scanf("%d",&n);
        for(int i=0;i<n;++i)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            seg[i]=line(point(x1,y1),point(x2,y2));
        }
        if(n==1) flag=true;
        for(int i=0;i<n&&!flag;++i)
            for(int j=i+1;j<n;++j)
            {
                if(check(seg[i].s,seg[j].s)||check(seg[i].s,seg[j].e)||check(seg[i].e,seg[j].s)||check(seg[i].e,seg[j].e))
                {
                    flag=true;
                    break;
                }
            }
        if(flag)
           puts("Yes!");
        else
           puts("No!");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值