###### 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;
}

#### 神州数码笔试题目(二)

2007-11-07 13:42:00

#### POJ 1127 基础计算几何（判断两线段相交）+并查集

2017-07-15 16:03:51

#### POJ1410Intersection【判断线段与矩形相交+点在矩形内的简单判定】

2015-09-04 09:58:10

#### 判断两条直线 共线或 平行 或相交 求交点 叉乘的应用 poj 1269 Intersecting Lines

2014-06-26 15:06:52

#### 如何判断直线之间和直线与平面之间的关系

2017-03-03 14:19:29

#### poj 2653代码 判断两直线是否相交

2013年09月15日 5KB 下载

#### JAVA版判断两条线是否相交

2013-05-15 10:58:06

#### 判断两条线段是否相交 计算几何

2016-12-08 21:31:14

#### 两条平行线怎样相交

2015-12-23 09:26:37

#### POJ 1269 Intersecting Lines（直线相交？平行？重合？，求交点）

2017-04-13 15:53:07

## 不良信息举报

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