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

原创 2016年08月30日 22:34:59

纯用模版即可

#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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ1269-Intersecting Lines(判断两条直线平行、重合或者相交)

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8789...

POJ 1269 || Intersecting Lines(直线重合,平行,相交判定

poj 不能用%lf输出,被坑了五六七八WA,从70多行的代码改到一百多行,发现卧槽原来是这个wa点,无法吐槽自己。 没用模板,自己从头文件开始狂敲。 等下去学一个模板好像非常简单粗暴。 ...
  • FXXKI
  • FXXKI
  • 2015-03-26 20:10
  • 379

poj1269Intersecting Lines【直线相交平行共线的判断+求交点】

Language: Default Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissio...

POJ1269 简单的计算几何判断直线相交

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8898...

POJ - 1269:Intersecting Lines__判断两直线相交

Description We all know that a pair of distinct points on a plane defines a line and that a pai...

POJ 1269 Intersecting Lines 判断直线相交

#include "stdio.h" #include "math.h" #define EPS 1e-8 struct point { double x, y; }; struct line {...
  • f_cpp
  • f_cpp
  • 2014-11-11 11:01
  • 143

POJ 1269 Intersecting Lines 直线相交判断

D - Intersecting Lines Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d ...

POJ 1269 Intersecting Lines(简单判断直线关系)

题目大意:给出两条直线(由4个点确定),要求判断出这两条直线的关系:平行,同线,相交。如果相交还要求出交点坐标。思路:先判断两条直线是不是同线,不是的话再判断是否平行,再不是的话就只能是相交的,求出交...

poj 1269 判断两直线的关系并求交点

主要是利用向量的叉积,求交点的时候就是解二元一次方程组,没什么难度,采用斜率法算的因为精度损失过大,可能导致wrong #include #include #include #include ...

poj1269Intersecting Lines直线相交 模板

Description We all know that a pair of distinct points on a plane defines a line and that a pair ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)