Describe
http://poj.org/problem?id=1269
Give two straight lines to determine whether they are parallel, coincident, or intersecting, and if they intersect, find the intersection.
Analysis
Compared to a naked problem. Learn the notation of a straight line (parametric equation)
1#include <cstdio>2#include <cmath>3 using namespacestd;4 5 Const Doubleeps=1e-8;6 7 structpt{Doublex, y; PtDoublex=0,Doubley=0): X (x), Y (y) {}};8 typedef PT VT;9 intDCMP (Doublex) {if(Fabs (x) <eps)return 0;returnX>0?1:-1; }TenVtoperator+ (vt A,vt b) {returnVT (a.x+b.x,a.y+b.y); } OneVtoperator-(VT A,vt b) {returnVT (a.x-b.x,a.y-b.y); } AVtoperator* (VT A,DoubleP) {returnVT (a.x*p,a.y*p); } - DoubleCross (VT A,vt b) {returna.x*b.y-a.y*b.x;} - structline{ the PT P; VT v; - Line () {} -Line (PT A,pt b) {p=a; v=b-A;} - }; + intline_intersection (line A,line B) { - if(DCMP (A.V,B.V)! =0))return-1; + returnDCMP (Cross (A.V,A.P-B.P)) = =0; A } at pt Get_line_intersection (line A,line B) { -VT v=a.v,w=b.v,u=a.p-B.P; - DoubleT=cross (w,u)/Cross (v,w); - returna.p+v*T; - } - intMain () { in intN; -scanf"%d",&n); toPuts"intersecting LINES OUTPUT"); + while(n--){ -PT p[4]; Line l[2]; the for(intI=0;i<4; i++) scanf ("%LF%LF",&p[i].x,&p[i].y); *l[0]=line (p[0],p[1]); $l[1]=line (p[2],p[3]);Panax Notoginseng intT=line_intersection (l[0],l[1]); - if(t==-1){ thePT X=get_line_intersection (l[0],l[1]); +printf"Point %.2lf%.2lf\n", x.x,x.y); A } the Else if(t==1) puts (" Line"); + ElsePuts"NONE"); - } $Puts"END of OUTPUT"); $ return 0; -}
View Code
Intersecting Lines
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 13622 |
|
Accepted: 6060 |
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
Source
Mid-Atlantic 1996
Poj_1269_intersecting_lines_ (computational geometry basis)