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

POJ 1269 Intersecting Lines(计算几何) (两线段位置)

程序员文章站 2022-03-30 08:49:02
...


Intersecting Lines
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17299   Accepted: 7449

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


【题意】

判断两直线位置, 共线(重合)  平行  相交,  相交求交点

【思路】

    4个点, 两条直线,直线p1p2. ,p3p4  根据向量叉乘判断, 若共线,则有 p1p3 X p2p3 =0 && p1p4 X p2p4 =0  

p1p2p3三点共线, p1p2p4三点共线;

    若平行则有  向量 p1p2 X p3p4 =0  两直线共线

    若相交,  假设交点 p0(x0,y0);  有 

POJ 1269 Intersecting Lines(计算几何) (两线段位置) 

 p0p1 X p0p2=0  

 p0p3 X p0p4=0

则有:

      (y1-y2) x0 + (x2-x1) y0 + x1y2-x2y1 =0

      (y3-y4) x0 + (x4-x3) y0 + x3y4-x4y3 =0

 有   a1x + b1y +c1 =0   

       a2x+ b2y + c2 =0   联立方程解的 POJ 1269 Intersecting Lines(计算几何) (两线段位置)


[代码实现]

#include <iostream>
//#include <bits/stdc++.h>
#include <stdio.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define SHUT ios_base::sync_with_stdio(false);cout.precision(20);cout.setf(ios::fixed);cout.tie(nullptr);cin.tie(nullptr);
const int INF=0x3f3f3f3f;
const double esp=1e-8;
typedef long long ll;
typedef long double ld;
const int MAXN=1e6+5;
using namespace std;
struct node{
    double x,y;
};
typedef struct Segments{
    node S1,S2;
}Seg;
bool judge_parallel(node p1,node p2,node p3,node p4)
{
    return (p4.y-p3.y)*(p2.x-p1.x)-(p2.y-p1.y)*(p4.x-p3.x);
}
bool judge_Collinear(node p1,node p2,node p3)
{
    return (p3.y-p2.y)*(p3.x-p1.x)-(p3.x-p2.x)*(p3.y-p1.y);

}
void solve(node p1,node p2,node p3,node p4)
{
    ld a1=p1.y-p2.y;
    ld a2=p3.y-p4.y;
    ld b1=p2.x-p1.x;
    ld b2=p4.x-p3.x;
    ld c1=p1.x*p2.y-p2.x*p1.y;
    ld c2=p3.x*p4.y-p4.x*p3.y;
    double x0= (c1*b2-c2*b1)/(a2*b1-a1*b2);
    double y0= (a2*c1-a1*c2)/(a1*b2-a2*b1);
  //  cout<<"POINT "<<x0<<" "<<y0<<endl;
   // printf("POINT %.2lf %.2lf\n",x0,y0);
    printf("POINT %.2f %.2f\n",x0,y0);
}
int main()
{
    int t;
    cin>>t;
   // cout.precision(3);
    puts("INTERSECTING LINES OUTPUT");
    node p1,p2,p3,p4;
    while(t--)
    {
        cin>>p1.x>>p1.y;
        cin>>p2.x>>p2.y;
        cin>>p3.x>>p3.y;
        cin>>p4.x>>p4.y;
        if(judge_Collinear(p1,p2,p3)==0&&judge_Collinear(p1,p2,p4)==0) // 共线
                cout<<"LINE"<<endl;
        else
        {
            if(judge_parallel(p1,p2,p3,p4)==0)//平行
            {
                    cout<<"NONE"<<endl;
            }
            else
            {
                solve(p1,p2,p3,p4);// 求交点
            }
        }

    }
    puts("END OF OUTPUT");
    return 0;
}

123