Intersecting Lines直线相交

来源:互联网 发布:基于c语言的win32api 编辑:程序博客网 时间:2024/05/28 15:02
 Intersecting Lines
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

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

50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END 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-9typedef 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 100005struct 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
原创粉丝点击