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

纯用模版即可

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值