poj 3304 Segments (跟所有线段相交的直线)

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13902 Accepted: 4449

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!

思路转自:http://blog.sina.com.cn/s/blog_6635898a0100n2lv.html

题意:求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点。

 

思路:计算几何。这道题要思考到两点:

1:把问题转化为是否存在一条直线与每条线段都有交点。证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。

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

ps:要学会这种从结果反过来证的思考方法....

我的判断线段与直线l是否相交的方法:

1:利用叉积的性质,判断线段的两个端点是否在直线的两边。

2:求线段所在的直线tmp,求tmp与l的交点p,由线段两端点到p的距离之和,与线段的距离比较,若相等则证明线段与直线相交。



#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps = 1e-8;
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 xx, double yy) : x(xx), y(yy){}
    Point operator -(const Point &a) const
    {
        return Point(x-a.x, y-a.y);
    }
    double operator ^(const Point &a) const
    {
        return x*a.y - y*a.x;
    }
    double operator *(const Point &a) const
    {
        return x*a.x + y*a.y;
    }
};
struct Line
{
    Point s, e;
    Line(){}
    Line(Point ss, Point ee) : s(ss), e(ee){}
};
double xmult(Point p0, Point p1, Point p2)
{
    return (p1-p0)^(p2-p0);
}
bool Seg_line(Line a, Line b)
{
    return sgn(xmult(b.s,a.s,a.e))*sgn(xmult(b.e, a.s, a.e)) <= 0; //这里一定要注意,a,b的顺序。。因为下面是Seg_line(a, line[i]),所以a是直线,b是线段,看这个线段是不是在这条直线的两边,如果a,b倒过来,就是其余line的都是直线,a是线段了。。
}
double dist(Point a, Point b)
{
    return sqrt((b-a)*(b-a));
}
const int maxn = 110;
Line line[maxn];
bool check(Line a, int n)
{
    if(sgn(dist(a.s, a.e)) == 0) return false;  //一定要考虑重点
    for(int i = 0; i < n; i++)
        if(Seg_line(a, line[i]) == 0)
            return false;
    return true;
}
int main()
{
    int n, t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        double x1, x2, y1, y2;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            line[i] = Line(Point(x1, y1), Point(x2, y2));
        }
        int flag = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                if(check(Line(line[i].s,line[j].s), n) || check(Line(line[i].s,line[j].e),n) ||
                   check(Line(line[i].e,line[j].s),n) || check(Line(line[i].e,line[j].e),n))
                    {
                        flag = 1;
                        break;
                    }
            }
            printf("%s\n", flag ? "Yes!" : "No!");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值