zoukankan      html  css  js  c++  java
  • poj3304 Segments【计算几何】

    最近开始刷计算几何了 公式好多完全不会

    数学不行 几何不行 记忆力不行 当机

    查的题解 就当复习吧 这套专题拿来熟悉一下计算几何模板

    #include <iostream>
    #include<stdio.h>
    #include<math.h>
    
    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)//判断p0是否在p1-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;//线段b的两个端点在直线a的两端
    }
    
    double dist(point a, point b)
    {
        return sqrt((b - a) * (b - a));
    }
    
    const int maxn = 110;
    line ll[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, ll[i]) == 0)
                return false;
        }
        return true;
    }
    
    int main()
    {
        int n, t;
        cin>>t;
        while(t--){
            cin>>n;
            double x1, x2, y1, y2;
            for(int i = 0; i < n; i++){
                cin>>x1>>y1>>x2>>y2;
                ll[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(ll[i].s, ll[j].s), n) || check(line(ll[i].s, ll[j].e), n)||
                       check(line(ll[i].e, ll[j].s), n) || check(line(ll[i].e, ll[j].e), n)){
                            flag = true;//找到了一条直线可以和所有的线段相交
                            break;
                       }
                }
            }
            printf("%s
    ", flag ? "Yes!" : "No!");
        }
        return 0;
    }



  • 相关阅读:
    C# 用this修饰符为原始类型扩展方法
    windows7命令行终端获取管理员模式随笔
    C语言---斐波那契问题
    C语言--pow()函数实现
    数组排序之选择排序
    求数组逆置(数组与指针实现)
    字符串函数之Strtok()函数
    for循环的灵活性
    C语言--isspace()函数实现
    异构无线网络之QOS简介
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643416.html
Copyright © 2011-2022 走看看