poj 1269 Intersecting Lines 【直线相交】

来源:互联网 发布:c语言表白源代码 编辑:程序博客网 时间:2024/06/05 15:42



点击打开链接


题意:


         给你两个条直线,判断两条直线的关系,

         如果相交于一点,输出POINT 和该点坐标,

         如果重合输出 LINE

         如果平行输出 NONE


题解:


        赤裸裸的判断两线相交,,



#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<map>#include<cmath>#define x first#define y secondusing namespace std;const double eps=1e-8;const double PI=acos(-1.0);const int maxn=105;int sgn(double x){    if(fabs(x)<eps) return 0;    if(x<0) return -1;    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);    }    double operator ^ (const Point &b)const{        return x*b.y - y*b.x;    }};struct Line{    Point s,e;    Line(){}    Line(Point _s,Point _e){        s=_s;e=_e;    }    pair<int,Point> operator &(const Line &b)const{        Point res=s;        if(sgn((s-e)^(b.s-b.e))==0){            if(sgn((s-b.e)^(b.s-b.e))==0)                return make_pair(0,res);            return make_pair(1,res);        }        double tt=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));        res.x+=(e.x-s.x)*tt;        res.y+=(e.y-s.y)*tt;        return make_pair(2,res);    }};Point p1,p2,p3,p4;Line t1,t2;int main(){    int t,n,cnt;    double x1,x2,x3,x4,y1,y2,y3,y4;    scanf("%d",&t);    puts("INTERSECTING LINES OUTPUT");    while(t--){        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);        p1={x1,y1};p2={x2,y2};        p3={x3,y3};p4={x4,y4};        t1={p1,p2};t2={p3,p4};        pair<int,Point>ans=t1&t2;        if(ans.x==0) puts("LINE");        else if(ans.x==1) puts("NONE");        else {            printf("POINT %.2f %.2f\n",ans.y.x,ans.y.y);        }    }    puts("END OF OUTPUT");return 0;}