poj 3304 Segments 线段与直线相交

Segments
Time Limit: 1000MS   Memory Limit: 65536K
     

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!
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
#include<time.h>
using namespace std;
#define LL long long
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=2e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=998244353,MOD=998244353;
const double eps=1e-8,pi=(4*atan(1.0));

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.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 Cross(Point p0,Point p1,Point p2) //p0p1 X p0p2
{
    return (p1-p0)^(p2-p0);
}
bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
{
    return sgn(Cross(l2.s,l1.s,l1.e))*sgn(Cross(l2.e,l1.s,l1.e)) <= 0;
}
Point a[N],b[N];
double dist(Point a,Point b)
{
    return sqrt( (b - a)*(b - a) );
}
int check1(Line x,int n)
{
    if(sgn(dist(x.s,x.e))==0)return 0;
    for(int k=1;k<=n;k++)
    {
        Line now=Line(a[k],b[k]);
        if(!Seg_inter_line(x,now))return 0;
    }
    return 1;
}
int check(int n)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            Line l1=Line(a[i],a[j]),l2=Line(a[i],b[j]),l3=Line(b[i],a[j]),l4=Line(b[i],b[j]);
            if(check1(l1,n)||check1(l2,n)||check1(l3,n)||check1(l4,n))return 1;
        }
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            a[i]=Point(x1,y1),b[i]=Point(x2,y2);
        }
        if(check(n))printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}

 

posted @ 2017-09-14 11:12  jhz033  阅读(150)  评论(0编辑  收藏  举报