POJ-1269—Intersecting Lines(简单计算几何)

                                         Intersecting Lines

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19286   Accepted: 8196

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

Source

Mid-Atlantic 1996

题目大意:给你两条直线的两点坐标(两点确定一条直线),问你这两条直线的关系,如果平行输出NONE,如果是同一条直线输出LINE如果相交输出交点坐标

很简单的一道题,就是要判断的有点多:

一:两条直线存在斜率时(垂直于x轴)

1.横坐标也是一样则是相同的直线

2.横坐标不一样,则平行

二:一条存在·,一条不存在,则一定是相交的,把不存在的横坐标带入即可求解

三:都存在:

1.斜率相同

<1:截距b也相等,则是同一条直线

<2:截距不相同,则平行

2.斜率不同

计算交点即可

代码:

/*
给出四个点的坐标,判断两直线是相交还是平行,相交写出交点
*/
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define ul unsigned long long
#define inf 0x3f3f3f3f
#define esp 1e-6
#define mm(a,b) memset(a,b,sizeof(a))
#define T()int test,q=1,tt;scanf("%d",&test),tt=test;while(test--)
const int N=1e3+10;
const int maxn=1e6+100;
struct point
{
    double x,y;
};
struct line
{
    point a,b;
    double k,bb;
} l1,l2;
double K(line l)
{
    return (l.a.y-l.b.y)/(l.a.x-l.b.x);
}
double B(line l)
{
    return l.a.y-l.k*l.a.x;
}
bool truek(line l)///判断k是否存在
{
    if(l.a.x==l.b.x)///k不存在
        return false;
    return true;
}
int main()
{
    T()
    {
        if(q==1)
            printf("INTERSECTING LINES OUTPUT\n");
        int flag1=0,flag2=0,flag3=0;
        double x1,y1;
        cin>>l1.a.x>>l1.a.y>>l1.b.x>>l1.b.y>>l2.a.x>>l2.a.y>>l2.b.x>>l2.b.y;
        if(truek(l1))///l1存在
        {
            flag1=1;
            l1.k=K(l1);///直线a的斜率
        }
        l1.bb=B(l1);///直线a的b
        if(truek(l2))///l2存在
        {
            flag2=1;
            l2.k=K(l2);///直线b的斜率
        }
        l2.bb=B(l2);///直线b的b
        if(flag1==0&&flag2==0)///两直线均垂直于x轴
        {
            if(l1.a.x==l2.a.x)///b也一样
                flag3=3;
            else
                flag3=2;
        }
        else if((!flag1&&flag2)||(flag1&&!flag2))///有一个不存在
        {
            flag3=1;
            if(flag1)///直线1存在
            {
                x1=l2.a.x;
                y1=l1.k*x1+l1.bb;
            }
            else///直线2存在
            {
                x1=l1.a.x;
                y1=l2.k*x1+l2.bb;
            }
        }
        else ///都存在
        {
            if(l1.k==l2.k)
            {
                if(l1.bb==l2.bb)
                {
                    flag3=3;
                }
                else
                    flag3=2;
            }
            else
            {
                flag3=1;
            x1=(l2.bb-l1.bb)/(l1.k-l2.k);
            y1= l1.k*x1+l1.bb;
            }
        }
        if(flag3==1)///相交
        {
            printf("POINT %.2lf %.2lf\n",x1,y1);
        }
        else if(flag3==2)///平行
        {
            printf("NONE\n");
        }
        else if(flag3==3)///一样
        {
            printf("LINE\n");
        }
        if(q==tt)
            printf("END OF OUTPUT\n");
        q++;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lee371042/article/details/86063534