zoukankan      html  css  js  c++  java
  • POJ 3304 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 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.

    题目大意:给n条线段,问是否存在一条直线穿过所有线段

    思路:题目本身没什么难度,就是枚举所有穿过线段端点的直线即可,难是难在精度的控制。第一个要注意的地方是n=1的时候要特判,第二个要注意的是如果两点过于接近是不能作为枚举直线的,因为这样算出来的叉积过小,很容易小于EPS。注意这两点就没什么大问题了。

    #include <cstdio>
    #include <cmath>
    
    #define EPS 1e-10
    #define Abs(x) x>0?x:-x
    
    typedef double TYPE;
    //2D point
    struct Point {
        TYPE x, y;
        Point() {}
        Point(double xx, double yy): x(xx), y(yy) {};
    
        bool operator < (const Point &B) const {
            if(x == B.x) return y < B.y;
            else return x < B.x;
        }
    };
    //if return>0 then a is on the clockwise of b
    TYPE cross(const Point &a, const Point &b, const Point &o) {
        return (o.x - a.x) * (o.y - b.y) - (o.x - b.x) * (o.y - a.y);
    }
    //distance between a and b
    inline double dist(Point a, Point b){
        double x = a.x - b.x, y = a.y - b.y;
        return sqrt(x * x + y * y);
    }
    //2D twp-point form segment
    struct Seg {
        Point st, ed;
        Seg() {}
        Seg(Point aa, Point bb):st(aa), ed(bb) {}
    };
    //if a straddle b
    inline bool straddle(const Seg &A, const Seg &B) {
        return cross(B.st, A.st, B.ed) * cross(B.st, A.ed, B.ed) < EPS;
    }
    
    #define MAXN 110
    
    int T, n;
    Seg sg[MAXN];
    
    inline bool check(Seg p) {
        if(dist(p.st, p.ed) < EPS) return false;
        for(int i = 0; i < n; ++i)
            if(!straddle(sg[i], p)) return false;
        return true;
    }
    
    bool solve() {
        for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) {
            if(check(Seg(sg[i].st, sg[j].st))) return true;
            if(check(Seg(sg[i].st, sg[j].ed))) return true;
            if(check(Seg(sg[i].ed, sg[j].st))) return true;
            if(check(Seg(sg[i].ed, sg[j].ed))) return true;
        }
        return false;
    }
    
    int main() {
        scanf("%d", &T);
        while(T--) {
            scanf("%d", &n);
            for(int i = 0; i < n; ++i) scanf("%lf%lf%lf%lf", &sg[i].st.x, &sg[i].st.y, &sg[i].ed.x, &sg[i].ed.y);
            if(n == 1 || solve()) puts("Yes!");
            else puts("No!");
        }
    }
    

      

  • 相关阅读:
    信息安全系统设计基础第八周期中复习总结
    layui下各种富文本的冲突情况
    TP3.2+find_set_in 以及 find_set_in和like的区别
    tp5+linux+apache php7.1.30环境下,上传图片报错:mkdir():permission denied
    一次基于老古董thinkPHP3.1的修改尝试
    微信网页开发 thinkphp5.0的try-catch和重定向
    CentOS 7.2下服务器配置(linux+apache+php+mysql)
    微信小程序踩坑(不定时更新)
    PHP 定时自动执行代码
    PHP TP5 文章评论+积分+签到
  • 原文地址:https://www.cnblogs.com/oyking/p/3189886.html
Copyright © 2011-2022 走看看