zoukankan      html  css  js  c++  java
  • Segments--poj3304(判断直线与线段之间的关系)

    http://poj.org/problem?id=3304

    给你几条线段  然后 让你找到一条直线让他在这条直线上的映射有一个重合点  

    如果有这条直线的话  这个重合的部分的两个端点一定是某两条线段的端点

    所以只需要枚举每个点连成的直线能不能跟所有的线段相交就行了

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<math.h>
    #define N 200
    const double ESP = 1e-8;
    struct Point
    {
        double x, y;
    
        Point(double x=0,double y=0):x(x),y(y) {}
        Point operator + (const Point &temp)const{
            return Point(x+temp.x, y+temp.y);
        }
        Point operator - (const Point &temp)const{
            return Point(x-temp.x, y-temp.y);
        }
        bool operator == (const Point &temp)const{
            return (fabs(x-temp.x) < ESP && fabs(y-temp.y) < ESP);
        }
        int operator * (const Point &temp)const{
            double t=(x*temp.y)-(y*temp.x);
            if(t > ESP)
                return 1;
            if(fabs(t) < ESP)
                return 0;
            return -1;
        }
    };
    
    struct node
    {
        Point A,B;
        node(Point A=0,Point B=0):A(A),B(B){}
    
    };
    
    int Find(node t,node a[],int n)
    {
        for(int i=0;i<n;i++)
        {
            int k=fabs((a[i].A-t.A)*(t.B-t.A)+(a[i].B-t.A)*(t.B-t.A));
    
            if(k==2)
                return false;
        }
        return true;
    }
    
    int main()
    {
        int n, T;
        scanf("%d", &T);
        while(T--)
        {
            Point p[N];
            node a[N];
            scanf("%d", &n);
            double x1,x2,y2,y1;
            int b=0;
            for(int i=0;i<n;i++)
            {
                scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
                p[b++]=Point(x1,y1);
                p[b++]=Point(x2,y2);
                a[i]=node(p[b-2],p[b-1]);
            }
            int ok=0;
            for(int i=0; i<b && !ok; i++)
            {
                for(int j=i+1; j<b && !ok; j++)
                {
                    if(p[i] == p[j])
                        continue;
                    ok = Find(node(p[i],p[j]),a,n);
                }
            }
            if(ok)
                printf("Yes!
    ");
            else
                printf("No!
    ");
        }
        return 0;
    }
  • 相关阅读:
    assert用法,原理,改编(C++)
    使用临界段实现优化的进程间同步对象原理和实现 (转)
    去除表达式里面多余的()
    为什么C++编译器不能支持对模板的分离式编译
    python试题[转]
    从CSDN的趣味题学Python
    即时战略游戏中如何协调对象移动
    贪心算法精讲
    游戏引擎大全
    I/O 完成端口( Windows核心编程 )
  • 原文地址:https://www.cnblogs.com/linliu/p/5427618.html
Copyright © 2011-2022 走看看