关闭

POJ 1269 判断直线的关系:相交,平行,重合

74人阅读 评论(0) 收藏 举报
分类:

纯用模版即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

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);
}
vector operator * (vector A,double p) //向量数乘
{
    return vector(A.x*p , A.y*p);
}
vector operator / (vector A,double p)  //向量数除
{
    return vector(A.x/p , A.y/p);
}
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;
}
const double eps=1e-8;

int sgn(double x)        //浮点数比较大小函数
{
    if(fabs(x)<eps)
        return 0;
    else
        return x<0 ? -1:1;
}

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

int parallel(line l1,line l2)  //判断两直线是否平行
{
    if(sgn((l1.q-l1.p)^(l2.q-l2.p)) == 0)
        return 1;
    return 0;
}

point crosspoint(line l1,line l2) //直线相交求交点
{
    double a1=(l1.q-l1.p)^(l2.p-l1.p);
    double a2=(l1.q-l1.p)^(l2.q-l1.p);
    return point((l2.p.x*a2-l2.q.x*a1)/(a2-a1),(l2.p.y*a2-l2.q.y*a1)/(a2-a1));
}

int relation(point a,line l)
{
    int c=sgn((a-l.p)^(l.q-l.p));
    if(c>0)
        return 1;
    else if(c<0)
        return 2;
    else 
        return 3;     //点在直线上 
}

int judge(line l1,line l2)
{
    point ans;
    if(parallel(l1,l2)==0)
        return 2;
    else if(parallel(l1,l2)==1 && relation(l1.p,l2)==3) 
        return 1;
    else
        return 0;
}

int main()
{
    int N;
    int result;
    scanf("%d",&N);
    printf("INTERSECTING LINES OUTPUT\n");
    while(N--)
    {
        point ans;
        double x1,x2,x3,x4,y1,y2,y3,y4;
        line l1,l2;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
        l1.p=point(x1,y1);
        l1.q=point(x2,y2);
        l2.p=point(x3,y3);
        l2.q=point(x4,y4); 
        result=judge(l1,l2);
        if(result==0)
            printf("NONE\n");
        else if(result==1)
            printf("LINE\n");
        else 
        {
            ans=crosspoint(l1,l2);
            printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9770次
    • 积分:658
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条
    文章分类