欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 1269 Intersecting Lines

程序员文章站 2022-04-02 17:20:32
...

Intersecting Lines

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16865 Accepted: 7264

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


题意:
判断两条直线的位置关系。

代码:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

struct point
{
    double x,y;
};

struct line
{
    point p1,p2;
};

void solve(line l1,line l2)
{
    if(l1.p1.x==l1.p2.x)//l1斜率不存在
    {
        if(l2.p1.x==l2.p2.x)//l2斜率不存在
        {
            if(l1.p1.x==l2.p1.x)//l1与l2重合
                printf("LINE\n");
            else printf("NONE\n");//l1与l2平行
        }
        else//l1与l2相交
        {
            double xx,yy;
            xx=l1.p1.x;
            yy=(xx-l2.p1.x)*(l2.p2.y-l2.p1.y)/(l2.p2.x-l2.p1.x)+l2.p1.y;
            printf("POINT %.2f %.2f\n",xx,yy);
        }
    }
    else if(l2.p1.x==l2.p2.x)//l1斜率存在、l2斜率不存在
    {
        double xx,yy;
        xx=l2.p1.x;
        yy=(xx-l1.p1.x)*(l1.p2.y-l1.p1.y)/(l1.p2.x-l1.p1.x)+l1.p1.y;
        printf("POINT %.2f %.2f\n",xx,yy);
    }
    else//l1、l2斜率都存在
    {
        double xx,yy;
        double x11,x12,x21,x22,y11,y12,y21,y22;
        x11=l1.p1.x;
        x12=l1.p2.x;
        x21=l2.p1.x;
        x22=l2.p2.x;
        y11=l1.p1.y;
        y12=l1.p2.y;
        y21=l2.p1.y;
        y22=l2.p2.y;
        double k1,k2,b1,b2;
        k1=(y12-y11)/(x12-x11);
        b1=y11-k1*x11;
        k2=(y22-y21)/(x22-x21);
        b2=y21-k2*x21;
        if(k1==k2)//l1、l2平行
        {
            if(b1==b2) printf("LINE\n");
            else printf("NONE\n");
        }
        else//l1、l2相交
        {
            xx=(b2-b1)/(k1-k2);
            yy=(xx-l1.p1.x)*(l1.p2.y-l1.p1.y)/(l1.p2.x-l1.p1.x)+l1.p1.y;
            printf("POINT %.2f %.2f\n",xx,yy);
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    printf("INTERSECTING LINES OUTPUT\n");
    while(t--)
    {
        line l1,l2;
        scanf("%lf%lf%lf%lf",&l1.p1.x,&l1.p1.y,&l1.p2.x,&l1.p2.y);
        scanf("%lf%lf%lf%lf",&l2.p1.x,&l2.p1.y,&l2.p2.x,&l2.p2.y);
        solve(l1,l2);
    }
    printf("END OF OUTPUT\n");
}