POJ 3304 Segment 直线与线段相交

C - Segments
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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 x1y1x2y2 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条线段,问是否存在一条直线使得n条线段在此直线的投影相交
思路:
考虑n的规模比较小,n3可以过,在考虑如存在一条直线,必然存在这条直线的一条垂线与n条线段相交,因此为题等价于求解是否存在一条直线与n条线段相交
如果存在这样一条直线,那么通过适当旋转,肯定使这条直线与n条线段的两个端点相交
枚举n条线段的两个端点,判断由两点组成的直线是否与n条线段相交
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>

using namespace std;

#define eps 1e-10
#define maxn 110
const double pi=acos(-1.0);

double fabs(double x) {
    return x<0?-x:x;
}
int equal(double x,double y) {
    return fabs(x-y)<eps;
}
double sqr(double x) {
    return x*x;
}
int cmp(double x) {
    return x<-eps?-1:x>eps;
}


struct point {
    double x,y;
    point() {}
    point(double a,double b):x(a), y(b){}
    void input() { scanf("%lf%lf",&x,&y);}
    friend point operator - (const point &a,const point &b) {
        return point(a.x-b.x,a.y-b.y);
    }
    friend point operator + (const point &a,const point &b) {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator * (const point &a,const double r) {
        return point(a.x*r,a.y*r);
    }
    friend point operator / (const point &a,const double r) {
        return point(a.x/r,a.y/r);
    }
    friend int operator == (const point &a,const point &b) {
        return equal(a.x,b.x) && equal(a.y,b.y);
    }
    double norm(){
        return sqrt(sqr(x)+sqr(y));
    }
};
double det(const point &a,const point &b) {
    return a.x*b.y-a.y*b.x;
}
double dot(const point &a,const point &b) {
    return a.x*b.x+a.y*b.y;
}
double dist(const point &a,const point &b) {
    return (a-b).norm();
}
point rotate_point(const point &p,const double A) {
    double tx = p.x, ty = p.y;
    return point(tx*cos(A) -ty*sin(A), tx*sin(A)+ ty*cos(A));
}
struct line {
    point a,b;
    line() {}
    line(point x,point y):a(x),b(y) {}
    line make_line(const point a,const point b) {
        return line(a,b);
    }
    double dis_point_segment(const point p,const point s,const point t) {
        if( cmp(dot(p-s,t-s))<=0) return (p-s).norm();
        if( cmp(dot(p-t,s-t))<=0) return (p-t).norm();
        return fabs( det(s-p,t-p)/dist(s,t));
    }
    void PointProline(const point p,const point s,const point t,point &cp) {
        double r=dot(t-s,(p-s)/dot(t-s,t-s));
        cp=s+(t-s)*r;
    }
    bool PointonSegment(const point p,const point s,const point t) {
        return cmp(det(p-s,t-s))==0&& cmp(dot(p-s,p-t))<=0;
    }
    bool parallel(line a,line b) {
        return !cmp(det(a.a-a.b,b.a-b.b));
    }
    //直线
    bool line_make_point(line a,line b,point &res) {
        if(parallel(a,b)) return false;
        double s1=det(a.a-b.a,b.b-b.a);
        double s2=det(a.b-b.a,b.b-b.a);
        res=(a.b*s1-a.a*s2)/(s1-s2);
        return true;
    }
    line move_d(line a,const double &len) {
        point d=a.b-a.a;
        d=d/d.norm();
        d=rotate_point(d,pi/2);
        return line(a.a+d*len,a.b+d*len);
    }
};
bool judge(line tmp,line t) {
    return cmp(det(t.a-tmp.a,tmp.b-tmp.a))*cmp(det(t.b-tmp.a,tmp.b-tmp.a))<=0;
}
line f[maxn];
point p[2*maxn];
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int n; int flag=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        f[i].a.input(),f[i].b.input(),p[i]=f[i].a,p[i+n]=f[i].b;
        for(int i=0;i<2*n;i++) {
            for(int j=i+1;j<2*n;j++) {
                if(dist(p[i],p[j])>eps){
                    int t=0;
                    for(t=0;t<n;t++) {
                        if(!judge(line(p[i],p[j]),f[t]))
                           break;
                    }
                    if(n==t) {
                        flag=1;break;
                    }
                }
            }
            if(flag)
            break;
        }
        if(flag)
        puts("Yes!");
        else
        puts("No!");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值