【POJ】poj 3304 (计算机图论_直线和线段相交)

 

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!

 

 

 

这个题目是目的是找一条直线是否能和所有的线段相交,可以证明如果存在那么必有一条过所有线段的端点的其中两个,可以通过判断直线与线段的相交关系来判断是否存在这一条直线;注意,用到判断两个double之间的差值大小,

代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MAX 105
#define INF 1e-8
using namespace std;
const double PI = acos(-1.0);
struct point
{
    double x; double y;
}p[MAX*2];
int num;
double cross ( point& a, point& b, point& c)
{
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int sig( double d)/// 判断两个double之间的差值;
{
    return ( d > INF)-(d < -INF);
}
bool linecross( point& a,  point& b,  point& c, point& d)
{
    int d1, d2;
    d1 = sig(cross(a,b,c));
    d2 = sig(cross(a,b,d));
    if ((d1^d2) == -2 )///两条直线规范相交,(d1^d2) == -2表示d1,d2异号;
        return 1;
    if (d1 == 0||d2 == 0)///表示两条直线不规范相交;
        return 1;
    return 0;
}
bool Test(point& a, point& b)
{
    if ( sig(a.x-b.x)==0&&sig(a.y-b.y)==0 )///如果两个点离得很近就看做两个点重合,看做一个点;
        return false;
    for ( int i = 1; i <= 2*num; i += 2)
    {
        if (!linecross(a,b,p[i],p[i+1]))///判断a,b,点组成的这条线是否能和所有的线段相交
            return false;
    }
    return true;
}
bool Find ()
{
    for ( int i = 1; i <= 2*num; i++)
    {
        for ( int j = i+1; j <= 2*num; j++)
        {
            if ( Test(p[i],p[j])) return true;
        }
    }
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int test;
    cin >> test;
    while (test--)
    {
        cin >> num;
        for ( int i = 1; i <= num*2; i++)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
        }
        if (Find())
            printf("Yes!\n");
        else
            printf("No!\n");
    }
}

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值