Poj-1269-Intersecting Lines

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

这道题的大概意思是给你两个线段的两个端点的坐标,然后让你判断过这两条线段的直线的关系,平行而不重合,输出NONE,重合输出LINE,相交则输出POINT +交点坐标,开始输出答案前需输出INTERSECTING LINES OUTPUT,输出所有答案后输出END OF OUTPUT

思路,这道题主要是先判断两条直线的斜率是否存在,即把直线为x=n的情况单独讨论,如果斜率存在,再用两条直线的斜率判断两条直线是否平行,如果不平行,则求其交点,如果平行,则把一条线段的一个端点与另一条线段的一个端点连起来,这样就能又形成两条直线,判断这两条直线的斜率是否相等,相等则原来的两条直线重合,不相等的话则原来的两条直线平行不重合,另外,在原来两条直线是否重合的时候,要对斜率为零的情况先进行单独讨论,因为有一种特殊情况,如果给出两条直线的线段端点坐标分别为(1,2),(4,2)和(1,3),(4,3)这样的数据,我们无法用(1,2),(1,3)和(4,2),(4,3)这样新形成的斜率不存在的直线判断其是否重合,故要先进行单独判断。

代码如下:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
struct note{
    double x1;
    double y1;
    double x2;
    double y2;
};
struct note a[3];
//对斜率不存在的情况进行单独讨论
int xielv(note b0,note b1)
{
    if(b0.x1==b0.x2&&b1.x1==b1.x2&&b1.x1==b0.x1)
    {
        printf("LINE\n");
        return 1;
    }
    else if(b0.x1==b0.x2&&b1.x1!=b1.x2)
    {
        double k,m,x,y;
        k=(b1.y2-b1.y1)/(b1.x2-b1.x1);
        m=b1.y2-b1.x2*k;
        x=b0.x1;
        y=x*k+m;
        printf("POINT %.2lf %.2lf\n",x,y);
        return 1;
    }
    else if(b0.x1!=b0.x2&&b1.x1==b1.x2)
    {
        double k,m,x,y;
        k=(b0.y2-b0.y1)/(b0.x2-b0.x1);
        m=b0.y2-b0.x2*k;
        x=b1.x1;
        y=x*k+m;
        printf("POINT %.2lf %.2lf\n",x,y);
        return 1;
    }
    else
        return 0;


}
//假如两条直线平行,则判断其是否重合
void pingxing(note b0,note b1)
{   
    //对两条直线都是y=n的情况先进行判断
    if(b0.y1==b0.y2&&b1.y1==b1.y1&&b1.y1==b0.y1)
        printf("LINE\n");
    else if(b0.y1==b0.y2&&b1.y1==b1.y1&&b1.y1!=b0.y1)
        printf("NONE\n");
    //判段四点是否在一条直线上
    else if((b1.y2-b0.y2)/(b1.x2-b0.x2)==(b1.y1-b0.y1)/(b1.x1-b0.x1)||(b1.y2-b0.y2)/(b1.x2-b0.x2)==(b0.y1-b1.y1)/(b0.x1-b1.x1))
        printf("LINE\n");
    else
        printf("NONE\n");
    return;
}
//判断两条直线是否平行
void xiangjiao(note b0,note b1)
{
    if((b0.y2-b0.y1)/(b0.x2-b0.x1)==(b1.y2-b1.y1)/(b1.x2-b1.x1)||(b0.y2-b0.y1)/(b0.x2-b0.x1)==(b1.y1-b1.y2)/(b1.x1-b1.x2))
        //平行的话判断其是否重合
        pingxing(b0,b1);
    else
    {
        //不平行则联立两个方程求出交点坐标
        double m1,m2,k1,k2,x,y;
        k1=(b0.y2-b0.y1)/(b0.x2-b0.x1);
        k2=(b1.y2-b1.y1)/(b1.x2-b1.x1);
        m1=b0.y2-b0.x2*k1;
        m2=b1.y2-b1.x2*k2;
        x=(m1-m2)/(k2-k1);
        y=x*k1+m1;
        printf("POINT %.2lf %.2lf\n",x,y);
    }
    return;
}
int main()
{
    int T;
    scanf("%d",&T);
    printf("INTERSECTING LINES OUTPUT\n");
    while(T--)
    {
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&a[0].x1,&a[0].y1,&a[0].x2,&a[0].y2,&a[1].x1,&a[1].y1,&a[1].x2,&a[1].y2);
        //对直线斜率不存在的情况进行特判
        if(xielv(a[0],a[1]))
            continue;
        xiangjiao(a[0],a[1]);
    }
    printf("END OF OUTPUT\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值