# POJ 1269 Intersecting Lines 判断直线关系（相交（求交点），重合，平行）

（y1 - y2）x + (x2 - x1)y = x2y1 - x1y2;

（y3 - y4）x + (x4 - x3)y = x4y3 - x3y4;

a1x + b1y + c1 = 0;

a2x + b2y + c2 = 0;

D = a1b2 - a2b1; D1 = b1c2 - b2c1; D2 = c1a2 - c2a1;

D != 0时， x = D1 / D, y = D2 / D； 得到交点坐标。

D = 0时；如果a1*x3+b1*y3+c1==0（第二条直线上的点满足第一条直线的方程） , 则四个点在一条直线上；否则两直线平行，无交点。

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
struct point
{
double x, y;
};
point p[5];
double a1, a2, b1, b2, c1, c2, d;
int main()
{
int cas, i;
scanf("%d",&cas);
printf("INTERSECTING LINES OUTPUT\n");
while(cas--)
{
for(i=1;i<=4;i++)
scanf("%lf%lf",&p[i].x, &p[i].y);
a1 = p[1].y - p[2].y;
a2 = p[3].y - p[4].y;
b1 = p[2].x - p[1].x;
b2 = p[4].x - p[3].x;
c1 = p[1].x * p[2].y - p[1].y * p[2].x;
c2 = p[3].x * p[4].y - p[3].y * p[4].x;
d = a1*b2 - a2*b1;
if(!d)
{
if(a1 * p[3].x + b1 * p[3].y + c1 ==0)printf("LINE\n");
else printf("NONE\n");
}
else
{
double x = (b1*c2 - b2*c1)/d;
double y = (c1*a2 - a1*c2)/d;
printf ("POINT %.2f %.2f\n", x, y);

}
}
printf("END OF OUTPUT\n");
return 0;
}

写成函数，以后根据情况修改一下就变模板了

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct point
{
double x, y;
};

struct seg
{
point a, b;
};

/*double cross(point o, point p1, point p2)
{
return (p1.x - o.x )*(p2.y -o.y) - (p1.y - o.y)*(p2.x - o.x);
} */

void line_cross(point p1, point p2, point p3, point p4)
{
double a1, a2, b1, b2, c1, c2, d;
point ret;
a1 = p1.y - p2.y;
b1 = p2.x - p1.x;
c1 = p1.x * p2.y - p1.y * p2.x;
a2 = p3.y - p4.y;
b2 = p4.x - p3.x;
c2 = p3.x * p4.y - p3.y * p4.x;
d = a1 * b2 - a2 * b1;
if(!d)
{
if( a1*p3.x + b1*p3.y + c1 == 0)printf("LINE\n");
else printf("NONE\n");
return;
}
ret.x = (b1*c2 - b2*c1)/d;
ret.y = (c1*a2 - c2*a1)/d;
printf("POINT %.2f %.2f\n", ret.x, ret.y);
}

point p[5];

int main()
{
int cas, i;
scanf("%d",&cas);
printf("INTERSECTING LINES OUTPUT\n");
while(cas--)
{
for(i=1;i<=4;i++)
scanf("%lf%lf",&p[i].x, &p[i].y);
line_cross(p[1], p[2], p[3], p[4]);
}
printf("END OF OUTPUT\n");
return 0;
}

03-07 1595
04-13 410
05-16 697
08-27 38
07-30 675
08-03 70
10-28 287
08-13 448
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客