Poj3304 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 x1y1 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、把问题转化为是否存在一条直线与每条线段都有交点。证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。

2、枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。证明:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l满足题意,而且经过其中两线段的端点。

代码:

#include<iostream>
#include<cmath>
using namespace std;
const int Max = 105;
const double eps = 1e-8;
 
struct Point
{
    double x, y;
}s[Max], e[Max];
int n;
 
double mult(Point p1, Point p2, Point p0) 
{
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
 
bool run(Point p1, Point p2)
{
    if(abs(p1.x-p2.x) < eps && abs(p1.y-p2.y) < eps) 
        return false;
    for(int i = 0; i < n; i ++)
        if(mult(p1, p2, s[i]) * mult(p1, p2, e[i]) > eps) return false;
    return true;
}
 
int main()
{
    int t;
    cin >> t;
    for(int Z = 1; Z <= t; Z ++)
    {
        cin >> n;
        for(int i = 0; i < n; i ++)
            cin >> s[i].x >> s[i].y >> e[i].x >> e[i].y;
        bool flag = false;
        if(n < 3) flag = true;
        for(int i = 0; i < n && !flag; i ++)
            for(int j = i + 1; j < n && !flag; j ++)
            {
                if(run(s[i], s[j])) flag = true;
                else if(run(s[i], e[j])) flag = true;
                else if(run(e[i], s[j])) flag = true;
                else if(run(e[i], e[j])) flag = true;
            }
           if(flag) cout << "Yes!" << endl;
           else cout << "No!" << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值