Segments POJ - 3304 (计算几何,线段与直线相交的判断)

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 xyxy2 follow, in which (x1, y1) and (x2, y2) 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.

Sample Input

3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0

Sample Output

Yes!
Yes!
No!

思路:转化为选取线段上的两个端点,构成直线,去判断能否与其他线段都相交就行。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+5;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
const double eps=1e-8;
const double PI=acos(-1.0);
int sgn(double x)//定义符号函数
{
    if(fabs(x)<eps)
        return 0;
    else if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    double x,y;
    Point(){};//默认构造函数
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator-(const Point &b) const
    {
        return Point(x-b.x,y-b.y);
    }
    Point operator+(const Point &b) const
    {
        return Point(x+b.x,y+b.y);
    }
    double operator^(const Point &b) const//重载叉积
    {
        return x*b.y-y*b.x;
    }
    double operator*(const Point &b) const//点积
    {
        return x*b.x+y*b.y;
    }
    void transXY(double B)//绕原点旋转B弧度后,XY的值
    {
        double tx=x,ty=y;
        x=tx*cos(B)-ty*sin(B);
        y=tx*sin(B)+ty*cos(B);
    }
};
struct Line
{
    Point s,e;
    double k;
    Line()=default;
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
        k = atan2(e.y - s.y,e.x - s.x);//斜率
    }
    //两条直线求交点,
    //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
    //只有第一个值为2时,交点才有意义
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0)//如果两条线的叉积等于0,那么要么具有平行关系,要么重合
        {
            if(sgn((s-b.e)^(b.s-b.e)) == 0)//分别取两条边的任意两个点,进行判断即可
                return make_pair(0,res);//重合
            else return make_pair(1,res);//平行
        }
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));//利用正弦定理:a/sinA=b/sinB=c/sinC,分析可知,利用叉乘可以得出sin值
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
}L[maxn];
double xmult(Point p0,Point p1,Point p2) //p0p1 X p0p2
{
    return (p1-p0)^(p2-p0);
}
bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
{
    return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= 0;
}
double dist(Point a,Point b)
{
    return sqrt( (b - a)*(b - a) );
}
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_inter_line(a,L[i])==0)
        {
            return false;
        }
    }
    return true;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            L[i]=Line(Point(x1,y1),Point(x2,y2));
        }
        bool flag=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(check(Line(L[i].s,L[j].s),n) || check(Line(L[i].s,L[j].e),n)
                        || check(Line(L[i].e,L[j].s),n) || check(Line(L[i].e,L[j].e),n) )
                {
                    flag = true;    
                    break;
                }
            }
        }
        if(flag==1)
        {
            printf("Yes!\n");
        }
        else
        {
            printf("No!\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81915081