Intersecting Lines(两条线段的位置关系)

平行不共线

平行共线

相交并求交点

Intersecting Lines

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

若v1与v2的外积v1×v2为0,则两条线段平行,有可能存在部分重合。

再判断两条平行线段是否共线,方法是用L1的一端和L2的一端构成向量vs并与v2作外积,如果vs与v2也平行则两线段共线(三点共线)。

在共线的前提下,若起点较小的线段终点大于起点较大的线段起点,则判定为部分重合。

————————————————————————————————————————————

相交:两条线段的两个端点坐标(x1,y1) (x2,y2) (x3,y3) (x4,y4)

  b1=(y2-y1)*x1+(x1-x2)*y1

  b2=(y4-y3)*x3+(x3-x4)*y3

  D=(x2-x1)(y4-y3)-(x4-x3)(y2-y1)

  D1=b2*(x2-x1)-b1*(x4-x3)

  D2=b2*(y2-y1)-b1*(y4-y3)

  交点(x0,y0)

  x0=D1/D   y0=D2/D

推导:

当判定两条线段相交后,可以进行交点的求解,求交点可以用平面几何方法,列点斜式方程来完成。

但由于点斜式方程难以处理斜率为0的特殊情况,不方便求解。因而,参用向量法求解交点。

设交点为(x0,y0),则下列方程组成立:

根据以上方程组,消除参数k1和k2,得到如下方程:

然后求解(x0,y0),结果如下所示:

 

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
const int d=1e-8;
int n;
struct note
{
    double x1,y1,x2,y2,x3,y3,x4,y4;
}e[15];
int check(int i)
{
    if((int)(e[i].x2-e[i].x1)*(e[i].y3-e[i].y4)-(e[i].x3-e[i].x4)*(e[i].y2-e[i].y1)==0)
        return 0;
    return 1;
}
int check2(int i)
{
    if((int)(e[i].x3-e[i].x1)*(e[i].y3-e[i].y4)-(e[i].x3-e[i].x4)*(e[i].y3-e[i].y1)==0)
        return 0;
    return 1;
}
void point(int i)
{
    double b1=(e[i].y2-e[i].y1)*e[i].x1+(e[i].x1-e[i].x2)*e[i].y1;
    double b2=(e[i].y4-e[i].y3)*e[i].x3+(e[i].x3-e[i].x4)*e[i].y3;
    double D=(e[i].x2-e[i].x1)*(e[i].y4-e[i].y3)-(e[i].x4-e[i].x3)*(e[i].y2-e[i].y1);
    double D1=b2*(e[i].x2-e[i].x1)-b1*(e[i].x4-e[i].x3);
    double D2=b2*(e[i].y2-e[i].y1)-b1*(e[i].y4-e[i].y3);
    double x0,y0;
    x0=D1/D;
    y0=D2/D;
    cout<<fixed<<setprecision(2);
    cout<<"POINT "<<x0<<' '<<y0<<endl;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>e[i].x1>>e[i].y1>>e[i].x2>>e[i].y2;
        cin>>e[i].x3>>e[i].y3>>e[i].x4>>e[i].y4;
    }
    puts("INTERSECTING LINES OUTPUT");
    for(int i=1;i<=n;i++)
    {
        if(!check(i))
        {
            if(!check2(i))
                puts("LINE");
            else
                puts("NONE");
        }
        else
        {
            point(i);
        }
    }
    puts("END OF OUTPUT");
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值