poj 3304 Segments(计算几何)

Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12070   Accepted: 3819

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!

Source

Amirkabir University of Technology Local Contest 2006

[Submit]   [Go Back]   [Status]   [Discuss]

题目大意:给出平面内的n条线段,求是否存在一条直线使得所有线段的投影在直线上有至少一个共同的交点。

题解:这道题直接求解可能不太好搞,但是我们可以逆着想,如果存在一条直线使得所有线段的投影在直线上有共同的交点,那么一定存在一条与该投影直线垂直的直线与所有线段都有交点。因为只有100条线段,所我们可以暴力枚举线段中的两个端点,因为两点确定一条直线,利用确定的直线与所有线段判断是否相交,就能得到答案。为什么可以用端点确定的直线来判定呢?因为如果存在一条与所有线段都相交的直线,那么这条直线通过平移或者旋转的方式一定可以通过至少某两个端点。(实在想不过来就画画图)

如果线段P1P2和直线Q1Q2相交,则P1P2跨立Q1Q2,即:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0。

用这个公式判定即可,注意叉积不满足交换率,所以公式必须严格满足。

还需注意如果枚举到的端点重合,则无法确定一条直线,此时不能用这时的结果去判断。

#include    
#include    
#include    
#include    
#include    
#define N 130    
#define LL long long    
using namespace std;    
int t,m,n,cnt;    
struct point    
{    
    double x,y;    
    point(){x=y=0;};    
    point operator -(const point &a)    
    {    
        point t;     
        t.x=x-a.x; t.y=y-a.y;    
        return t;    
    }    
    bool operator ==(const point &a)const
    {
    	return x==a.x&&y==a.y;
    }
};point p[4*N];    
struct segment    
{    
    double  p1,p2,q1,q2;    
    segment(){p1=p2=q1=q2=0;};    
};segment a[N];    
struct vector    
{    
    double x,y;    
    vector(){x=y=0;};    
    vector operator =(const point &a)    
    {    
        this->x=a.x; this->y=a.y;    
        return *this;    
    }    
    double operator *(const vector &a)    
    {    
        return (double)x*a.y-(double)y*a.x;    
    }    
};    
bool solve(int x,int  y)    
{    
    vector t,t1;    
    t=p[y]-p[x];    
    if (p[y]==p[x]) return false;
    for (int i=1;i<=cnt;i+=2)    
     {    
        vector k; k=p[i]-p[x];    
        vector l; l=p[i+1]-p[x];   
        if ((k*t)*(t*l)<0) return false;    
     }    
    return true;    
}    
int main()    
{      
    scanf("%d",&t);    
    for (int T=1;T<=t;T++)    
     {    
        scanf("%d",&n);    
        cnt=0;    
        for (int i=1;i<=n;i++)    
        {    
         scanf("%lf%lf%lf%lf",&a[i].p1,&a[i].p2,&a[i].q1,&a[i].q2);    
         cnt++; p[cnt].x=a[i].p1; p[cnt].y=a[i].p2;    
         cnt++; p[cnt].x=a[i].q1; p[cnt].y=a[i].q2;    
        }    
        bool flag=false;    
        for (int i=1;i<=cnt-1;i++)    
        {    
         bool f=false;    
         for (int j=i+1;j<=cnt;j++)    
           if (solve(i,j)) {    
            f=true;    
            break;    
           }    
         if (f) {    
            flag=true;    
            break;    
         }    
         }    
         if (flag) printf("Yes!\n");    
         else printf("No!\n");    
     }    
}    



你可能感兴趣的:(计算几何)