Intersecting Lines直线相交

 Intersecting Lines
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


 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 tex2html_wrap_inline35 . Thus each of these input lines represents two lines on the plane: the line through tex2html_wrap_inline37 and tex2html_wrap_inline39 and the line through tex2html_wrap_inline41 and tex2html_wrap_inline43 . The point tex2html_wrap_inline37 is always distinct from tex2html_wrap_inline39 . Likewise with tex2html_wrap_inline41 and tex2html_wrap_inline43 .

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
 
   
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-9
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
struct Point
{
    double x, y;
    Point () {}
    Point(double a, double b)
    {
        x = a;
        y = b;
    }
};
typedef Point Vec;
Vec operator + (Vec a, Vec b)//点加法
{
    return Vec(a.x + b.x, a.y + b.y);
}
Vec operator - (Vec a, Vec b)//点减法
{
    return Vec(a.x - b.x, a.y - b.y);
}
Vec operator * (Vec a, double p)//点与常数相乘
{
    return Vec(a.x * p, a.y * p);
}
bool operator == (Point a, Point b)//点相等判断
{
    return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;
}
inline double crossDet(Vec a, Vec b)//叉乘
{
    return a.x * b.y - a.y * b.x;
}
inline double dotDet(Vec a, Vec b)//点乘
{
    return a.x * b.x + a.y * b.y;
}
Vec getvec(Point a, Point b)
{
    return Point(b.x - a.x, b.y - a.y);
}
Point lineIntersect(Point P, Vec v, Point Q, Vec w)//直线相交返回交点
{
    Vec u = P - Q;
    double t = crossDet(w, u) / crossDet(v, w);
    return P + v * t;
}
inline bool onLine(Point x, Point a, Point b)//叉积为0三点共线 
{
    return sgn(crossDet(a - x, b - x)) == 0;
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    printf("INTERSECTING LINES OUTPUT\n");
    while (T--)
    {
        Point p[10];
        for (int i = 0; i < 4; i++)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        Vec a = getvec(p[0], p[1]);
        Vec b = getvec(p[2], p[3]);
        if (sgn(crossDet(a, b)) == 0 && 
            (onLine(p[0],p[2],p[3]))//叉积为0且有一点共线
            )
        {
            printf("LINE\n");
        }
        else if (sgn(crossDet(a, b)) == 0)//平行情况
        {
            printf("NONE\n");
        }
        else//套版不解释
        {
            printf("POINT %.2lf %.2lf\n", lineIntersect(p[0], p[0] - p[1], p[2], p[2] - p[3]));
        }

    }
    printf("END OF OUTPUT\n");

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值