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

      

  • 相关阅读:
    常见面试题
    性能测试注意点
    orm 事物的使用
    mvc 页面如何引用命名空间并且直接使用枚举类型对象
    ef 动态拼接参数查询
    ef指定字段更新
    jquery 如何传递对象本身
    整数除以整数后转成百分比并且保留一位小数
    sql 表变量的使用
    echart的label标签文字过长显示不全怎么办?
  • 原文地址:https://www.cnblogs.com/oyking/p/3189886.html
Copyright © 2011-2022 走看看