POJ 1410 线段相交模版

WA了好多次,有很多小陷阱,第一个是注意输入时的排序,第二注意线段在矩形内部的情况。
题目比较直接
模板抄错,wa了好多次,晕~~~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const double eps=1e-8;
int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    else
        return x<0 ? -1:1;
}
struct point
{
    double x,y;
    point(){}
    point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
};

typedef point vector;
vector operator + (vector A,vector B)   //向量相加
{
    return vector(A.x+B.x , A.y+B.y);
}
vector operator - (point A,point B)   //两点相减得到向量
{
    return vector(A.x-B.x , A.y-B.y);
}
double operator ^ (vector A,vector B)  //叉乘
{
    return A.x*B.y-A.y*B.x;
}
double operator * (vector A,vector B)  //点乘
{
    return A.x*B.x+A.y*B.y;
}

struct line
{
    point p,q;
    line() {}
    line(point _p,point _q)
    {
        p=_p;
        q=_q;
    }
};

int segcrossseg(line l1,line l2) //2 规范相交 1 不规范相交 0 不相交
{
    int d1=sgn((l1.q-l1.p)^(l2.p-l1.p));
    int d2=sgn((l1.q-l1.p)^(l2.q-l1.p));
    int d3=sgn((l2.q-l2.p)^(l1.p-l2.p));
    int d4=sgn((l2.q-l2.p)^(l1.q-l2.p));
    //cout<<d1<<endl;
    //cout<<d2<<endl;
    //cout<<d3<<endl;
    //cout<<d4<<endl;
    if((d1^d2)==-2 && (d3^d4)==-2)
        return 2;  
    return  (d1==0 && sgn((l2.p-l1.p)*(l2.p-l1.q))<=0) ||
            (d2==0 && sgn((l2.q-l1.p)*(l2.q-l1.q))<=0) ||
            (d3==0 && sgn((l1.p-l2.p)*(l1.p-l2.q))<=0) ||
            (d4==0 && sgn((l1.q-l2.p)*(l1.q-l2.q))<=0) ;
}

int judge(line a,line b,line c,line d,line l)
{
    //cout<<segcrossseg(l,a)<<endl;
    //cout<<segcrossseg(l,b)<<endl;
    //cout<<segcrossseg(l,c)<<endl;
    //cout<<segcrossseg(l,d)<<endl;
    if(segcrossseg(l,a)!=0)
        return 1;
    if(segcrossseg(l,b)!=0)
        return 1;
    if(segcrossseg(l,c)!=0)
        return 1;
    if(segcrossseg(l,d)!=0)
        return 1;
    return 0;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int N;
    scanf("%d",&N);
    while(N--)
    {
        int ans=0;
        double x1,x2,x3,x4,y1,y2,y3,y4;
        double t;
        line a,b,c,d,l;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
        if(x3>x4)
        {
            t=x3;x3=x4;x4=t;
        }
        if(y3<y4)
        {
            t=y3;y3=y4;y4=t;
        }
        //printf("(%lf %lf),(%lf,%lf)\n",x3,y3,x4,y4);
        if (max(x1,x2)<x4 && max(y1,y2)<y3 && min(x1,x2)>x3 && min(y1,y2)>y4)
            ans=1;
        else
        {
            l.p=point(x1,y1);
            l.q=point(x2,y2);
            a.p=point(x3,y3);
            a.q=point(x3,y4);
            b.p=point(x3,y4);
            b.q=point(x4,y4);
            c.p=point(x4,y4);
            c.q=point(x4,y3);
            d.p=point(x4,y3);
            d.q=point(x3,y3);
            ans=judge(a,b,c,d,l);
        }
        if(ans)
            printf("T\n");
        else
            printf("F\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值