POJ 1410 线段相交模版

来源:互联网 发布:formtalk数据 编辑:程序博客网 时间:2024/05/16 09:33

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
原创粉丝点击