B 判断两个线段是否相交

来源:互联网 发布:阿里云ntp 编辑:程序博客网 时间:2024/06/01 08:54

很简单的一道几何题。

本来应该1A。Wa了一次后,头脑就开始乱了,第一次Wa,完全就是没有特判垂直X轴的那种。

然后,比赛完就1A了。我擦。

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;struct Point{    double x,y;};struct Line{    Point a,b;};Line L1,L2;double f1(double x){    //if(L1.a.x==L1.b.x)    return (x*(L1.b.y-L1.a.y)+L1.a.y*(L1.b.x-L1.a.x)-L1.a.x*(L1.b.y-L1.a.y))/(L1.b.x-L1.a.x);}double f2(double x){    //if(L1.a.x==L1.b.x)    return (x*(L2.b.y-L2.a.y)+L2.a.y*(L2.b.x-L2.a.x)-L2.a.x*(L2.b.y-L2.a.y))/(L2.b.x-L2.a.x);}bool Judge(){    double tmp1=L2.a.y-f1(L2.a.x),tmp2=L2.b.y-f1(L2.b.x);    double tmp3=L1.a.y-f2(L1.a.x),tmp4=L1.b.y-f2(L1.b.x);//    printf("%lf %lf %lf %lf\n",tmp1,tmp2,tmp3,tmp4);    if(tmp1==0||tmp2==0||tmp3==0||tmp4==0) return true;    if(tmp1*tmp2<=0&&tmp3*tmp4<=0) return true;    return false;}int main(){    int T;    scanf("%d",&T);    while(T--&&scanf("%lf%lf%lf%lf %lf%lf%lf%lf",&L1.a.x,&L1.a.y,&L1.b.x,&L1.b.y,&L2.a.x,&L2.a.y,&L2.b.x,&L2.b.y)){        if(L1.a.x==L1.b.x&&L2.a.x!=L2.b.x){            double tmp=f2(L1.a.x);            if(tmp>=min(L2.a.y,L2.b.y)&&tmp<=max(L2.a.y,L2.b.y)){                printf("Interseetion\n");            }            else {                printf("Not Interseetion\n");            }        }        else if(L2.a.x==L2.b.x&&L1.a.x!=L1.b.x){//            printf("%lf %lf\n",L2.a.x,L2.b.x);            double tmp=f1(L2.a.x);            if(tmp>=min(L1.a.y,L1.b.y)&&tmp<=max(L1.a.y,L1.b.y)){                printf("Interseetion\n");            }            else {                printf("Not Interseetion\n");            }        }        else if(L1.a.x==L1.b.x&&L2.a.x==L2.b.x){            if(L1.a.x==L2.a.x){                printf("Interseetion\n");            }            else {                printf("Not Interseetion\n");            }        }        else {//            printf("Bingo!\n");            if(Judge()) printf("Interseetion\n");            else printf("Not Interseetion\n");        }    }    return 0;}

简单几何。

0 0
原创粉丝点击