POJ3304 segment(线段交判断)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const double inf = 1e-8;
int dcmp(double x){
    if(fabs(x) < 1e-8)
            return 0;
    if(x < 0)   return -1;
    return 1;//1为正,-1为负,0为等
}
struct point{
    double x , y;
    point(double x = 0,double y= 0):x(x),y(y){}

/************************************/
};
typedef point Vector;
point operator + (const Vector &a , const Vector &b){
    return Vector(a.x + b.x,a.y + b.y);
}
point operator - (const Vector &a , const Vector &b){
    return Vector(a.x - b.x,a.y - b.y);
}
point operator * (const Vector &a , const double &p){
    return Vector(a.x * p,a.y * p);
}
point operator / (const Vector &a , const double &p){
    return Vector(a.x / p,a.y / p);
}
bool operator > (const point &a,const point &b){
return a.x > b.x;
}
bool operator == (const point &a,const point &b){
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) ==0;
};
double cross(const Vector &a ,const Vector &b)
{

    return (a.x * b.y - a.y * b.x);
}
double cross(point a,point b,point o)
{
    return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
double dot(const Vector &a,const Vector &b)
{
    return (a.x * a.y + b.x * b.y);

}
struct segment{
    point s,e;
    segment(point a = point(0,0),point b = point(0,0)){
    s = a;e = b;
    }
};
point pnt[300];
segment seg[200];
int cnt;
int cases;
int lnum ;
bool OnSegment(point p,point a1,point a2)
{
  return dcmp(cross(a1-p,a2-p))==0&&dcmp(dot(a1-p,a2-p))<0; //线段包含端点时改成<=
}
bool cancross(segment &a,segment &b){
    //跨立实验
    double c1 = cross(b.e - a.e , a.s - a.e) ,
        c2 = cross(b.s - a.e , a.s - a.e),
        c3 = cross(a.e - b.e , b.s - b.e),
        c4 = cross(a.s - b.e , b.s - b.e);
    if(dcmp(c1)*dcmp(c2) < 0 && dcmp(c1)*dcmp(c2) < 0)
            return true;
    if(OnSegment(a.e,b.e,b.s)||OnSegment(a.s,b.e,b.s)||OnSegment(b.e,a.e,a.s)||OnSegment(b.s,a.e,a.s))
                return true;
            return false;
}

bool judge(segment p)
{
    point a(0,0) ,b(0,0);
    a = p.e;
    b = p.s;
    if(abs(a.x-b.x)<inf&&abs(a.y-b.y)<inf) return false;
    for(int i=0;i<lnum;++i)
    {
        if(cross(a,b,seg[i].s)*cross(a,b,seg[i].e)>inf)
           return false;
    }
    return true;
}
int main()
{

    double x1,y1,x2,y2;
    scanf("%d",&cases);
    while(cases --)
    {
        memset(seg,0,sizeof(seg));
        memset(pnt,0,sizeof(pnt));
        bool flag = false;
        scanf("%d",&lnum);
        for(int i = 0; i < lnum ;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            pnt[2*i] = point(x1,y1);
            pnt[2*i+1] = point(x2,y2);
            seg[i] = segment(pnt[2*i],pnt[2*i+1]);
        }
        int i,j;
        if(lnum ==1 )   flag =true;
        for(i = 0; i <= 2*lnum-2 && flag == false;i++)
            for(j = i ; j <= 2*lnum-2 && flag == false;j++)
                if(judge(segment(pnt[i],pnt[j])))
                    flag = true;
        if(flag)
                printf("Yes!\n");
        else
                printf("No!\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值