【POJ - 3304 Segments】 直线线段相交判断

C - Segments

 

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个线段,问是否存在一条直线,使得所有线段在直线上的投影有交集。

分析:题意可以进行一个很奇妙的转化。如果存在这样的一条直线,那么过这条直线做垂线,这条垂线一定和所有线段相交。可以简单地在纸上画一下试试,画出三条线段,找到一条与这三条线段相交的直线,做这条直线的垂线,会发现所有线段的投影都在那条垂线上,那条垂线就是我们要找到的。那么这条直线一定经过两个端点,所以我们枚举端点,判断和所有的直线相交情况,注意要把重合的点去掉。

代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long

using namespace std;
const int MX = 105;
const int mod = 1e9 + 7;
const int INF = 2e9 + 5;
const double eps = 1e-8;

int n;
struct Point{
    double x, y;
    Point(){}
    Point(double _x, double _y){
        x = _x;
        y = _y;
    }
};

struct Line{
    Point s, e;
    Line(){}
    Line(Point x, Point y){
        s = x;
        e = y;
    }
}line[MX];

int sgn(double x){
    if(fabs(x) < eps)   return 0;
    if(x < 0)   return -1;
    return 1;
}

double multi(Point p0, Point p1, Point p2){
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

double dis(Point a, Point b){
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

bool intersect(Line l1, Line l2){
    return sgn(multi(l2.s, l1.s, l1.e)) * sgn(multi(l2.e, l1.s, l1.e)) <= 0;
}

int check(Line l){
    if(sgn(dis(l.e, l.s)) == 0)   return 0;
    for(int i = 0; i < n; i++){
        if(!intersect(l, line[i]))  return 0;
    }
    return 1;
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        double x1, y1, x2, 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)) || check(Line(line[i].s, line[j].e)) ||
                    check(Line(line[i].e, line[j].s)) || check(Line(line[i].e, line[j].e))){
                    flag = 1;
                    break;
                }
            }
            if(flag)    break;
        }
        if(flag)    printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值