Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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 . Thus each of these input lines represents two lines on the plane: the line through
and
and the line through
and
. The point
is always distinct from
. Likewise with
and
.
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; }