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!");
        }
    }
    

      

  • 相关阅读:
    spring异常
    springboot+mybatis
    mybatis初识
    模板引擎Dot
    mysql数据库操作
    1. 安装Oracle,配置环境 2. 实现查询From子句 3. 实现查询where子句 4. 实现查询order by子句
    (1)Set集合 (2)Map集合 (3)异常机制
    (1)网络编程的常识 (2)基于tcp协议的编程模型 (3)tcp协议和udp协议的比较 (4)基于udp协议的编程模型
    (1)线程的常用方法 (2)线程的同步机制 (3)网络编程的常识
    (1)I/O流 (2)线程
  • 原文地址:https://www.cnblogs.com/oyking/p/3189886.html
Copyright © 2011-2022 走看看