Intersecting Lines - POJ 1269(判断平面上两条直线的关系)

时间:2023-03-08 16:50:37
分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了....
代码如下:
======================================================================================================================================
#include<math.h>
#include<algorithm>
#include<stdio.h>
using namespace std; const int MAXN = ;
const double EPS = 1e-; struct point
{///定义点
double x, y; point(double x=, double y=):x(x),y(y){}
point operator - (const point &t) const{
return point(x-t.x, y-t.y);
}
double operator * (const point &t) const{
return x * t.y - y * t.x;
}
};
struct segment
{
point A, B;
double a, b, c; void InIt(point t1, point t2)
{
A = t1, B = t2;
a = B.y - A.y;
b = A.x - B.x;
c = A.x*B.y-B.x*A.y;
}
}; bool Parallel(segment t1, segment t2)
{///是否平行
return fabs((t1.A-t1.B)*(t2.A-t2.B)) < EPS;
}
bool Collineation(segment t1, segment t2)
{///是否共线 return fabs((t1.A-t1.B)*(t2.A-t1.B)) < EPS && fabs((t1.A-t1.B)*(t2.B-t1.B)) < EPS;
}
point CrossPoint(segment t1, segment t2)
{
point t; t.x = (t1.c*t2.b-t2.c*t1.b) / (t1.a*t2.b-t2.a*t1.b);
t.y = (t1.c*t2.a-t2.c*t1.a) / (t1.b*t2.a-t2.b*t1.a); return t;
} int main()
{
int N; while(scanf("%d", &N) != EOF)
{
segment p1, p2;
point A, B; printf("INTERSECTING LINES OUTPUT\n");
while(N--)
{
scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);p1.InIt(A, B);
scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);p2.InIt(A, B); if(Collineation(p1, p2) == true)
printf("LINE\n");
else if(Parallel(p1, p2) == true)
printf("NONE\n");
else
{
point ans = CrossPoint(p1, p2);
printf("POINT %.2f %.2f\n", ans.x, ans.y);
}
}
printf("END OF OUTPUT\n");
} return ;
}