POJ 1269 Intersecting Lines

POJ_1269

    可以先用叉积判断两条直线是否共线或者平行,如果有交点再用解析几何的办法就交点即可。

#include<stdio.h>
#include<string.h>
#define zero 1e-8
double x1, y1, x2, y2, x3, y3, x4, y4;
double det(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
double fabs(double x)
{
return x < 0 ? -x : x;
}
int dcmp(double x)
{
if(fabs(x) < zero)
return 0;
if(x < 0)
return -1;
return 1;
}
void solve()
{
double x, y, k1, k2;
if(dcmp(det(x2 - x1, y2 - y1, x4 - x3, y4 - y3)) == 0)
{
if(dcmp(det(x2 - x1, y2 - y1, x3 - x1, y3 - y1)) == 0)
printf("LINE\n");
else
printf("NONE\n");
}
else
{
if(dcmp(x2 - x1) == 0)
{
x = x1;
k2 = (y4 - y3) / (x4 - x3);
y = k2 * (x - x3) + y3;
}
else if(dcmp(x4 - x3) == 0)
{
x = x3;
k1 = (y2 - y1) / (x2 - x1);
y = k1 * (x - x1) + y1;
}
else
{
k1 = (y2 - y1) / (x2 - x1);
k2 = (y4 - y3) / (x4 - x3);
x = (k1 * x1 - k2 * x3 - y1 + y3) / (k1 - k2);
y = k1 * (x - x1) + y1;
}
printf("POINT %.2lf %.2lf\n", x, y);
}
}
int main()
{
int t;
scanf("%d", &t);
printf("INTERSECTING LINES OUTPUT\n");
while(t --)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
solve();
}
printf("END OF OUTPUT\n");
return 0;
}


你可能感兴趣的:(intersect)