Intersecting Lines
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 12284 |
|
Accepted: 5495 |
Description
We all know this a pair of distinct points on a plane defines a line and that a pair of lines on a plane would intersect in One of three ways:1) no intersection because they is parallel, 2) intersect in a line because they is on top of one Other (i.e they is the same line), 3) intersect at a point. In the problem you'll use your algebraic knowledge to create a program that determines how and where the lines intersec T.
Your program would repeatedly read in four points that define, lines in the X-y plane and determine how and where the Li NES intersect. All numbers required by this problem would be reasonable, say between-1000 and 1000.
Input
The first line contains an integer N between 1 and ten describing how many pairs of lines is represented. The next N lines would 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, lines on the Plane:the line through (x1,y1) and (X2,y2) and the line Throug H (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'll then being one line of output for each pair of planar lines represented by a line of input, describing how the Lin Es 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 both Decima L 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
Test instructions: There are n sets of data, each set of data give two straight lines two points, ask whether the two lines intersect, if not intersect, if the collinear output line, parallel output none, intersect, output intersection coordinates
p1 p2 p3 : (P2-P1) X (P3-P1) = 0, no intersection to determine if parallel
#include <cstring> #include <cstdio> #include <iostream> #include <algorithm> #include < Cmath> #define EPS 1e-8using namespace std;struct point {Double X, y,} p[8];///fork product double ok (point A,point b,point c) { Return (c.x-a.x) * (B.Y-A.Y)-(b.x-a.x) * (C.Y-A.Y);} Collinear judgment bool Line (point A,point b,point c) {return ok (a,b,c) ==0;} The intersection determines that bool None () {return (P[1].Y-P[2].Y) * (p[3].x-p[4].x) = = (p[1].x-p[2].x) * (P[3].Y-P[4].Y);} Find intersection point Inter (Point U1,point u2,point v1,point v2) {point ret=u1; Double t= ((u1.x-v1.x) * (V1.Y-V2.Y)-(U1.Y-V1.Y) * (v1.x-v2.x))/((u1.x-u2.x) * (V1.Y-V2.Y)-(U1.Y-U2.Y) * (v1.x-v2.x)); ret.x+= (u2.x-u1.x) *t; ret.y+= (U2.Y-U1.Y) *t; return ret;} int main () {//freopen ("In.txt", "R", stdin); int n; while (cin>>n) {printf ("intersecting LINES output\n"); while (n--) {for (int i=1; i<=4; i++) scanf ("%lf%lff", &p[i].x,&p[i].y); if (line (p[1],p[3],p[4)) &&line (P[2],p[3],p[4]) {printf ("line\n"); Continue } if (None ()) {printf ("none\n"); Continue } point Ans=inter (P[1],p[2],p[3],p[4]); printf ("Point%.2f%.2f\n", ans.x,ans.y); } printf ("END of output\n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1269 intersecting Lines (computational geometry)