【计算几何】 poj1269 Intersecting Lines

Intersecting Lines
http://poj.org/problem?id=1269

Description

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect. 
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000. 

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT

判断线段与线段之间的位置关系,网上找到两篇解释的很好的,转发下。

http://blog.csdn.net/zxy_snow/article/details/6341282

http://blog.csdn.net/dreamvyps/article/details/6162690


#include<cstdio>
#include<cstring>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define inf 1e-8
struct line
{
    double a,b,c,d;
}a,b;
double x,y;
void intersection()
{
    double a1=a.b-a.d,b1=a.c-a.a,c1=a.a*a.d-a.c*a.b;
    double a2=b.b-b.d,b2=b.c-b.a,c2=b.a*b.d-b.c*b.b;
    x=(c1*b2-c2*b1)/(a2*b1-a1*b2);
    y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
}
int main()
{
    int t;
    puts("INTERSECTING LINES OUTPUT");
    scanf("%d",&t);
    for(;t--;)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.a,&a.b,&a.c,&a.d,&b.a,&b.b,&b.c,&b.d);
        if(abs((a.a-a.c)*(b.b-b.d)-(a.b-a.d)*(b.a-b.c))<inf)//判断是否平行
        {
            if(abs((a.c-a.a)*(b.b-a.b)-(a.d-a.b)*(b.a-a.a))<inf)//判断是否共线
               puts("LINE");
            else
               puts("NONE");
        }
        else
        {
            intersection();//计算交点
            printf("POINT %.2f %.2f\n",x,y);
        }
    }
    puts("END OF OUTPUT");
    return 0;
}


你可能感兴趣的:(【计算几何】 poj1269 Intersecting Lines)