「POJ1269」Intersecting Lines
Description
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.
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 x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (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 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
1 2 3 4 5 6 |
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
1 2 3 4 5 6 7 |
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT |
题解
求线段是否重合/平行/相交并求交点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define eps 1e-6 using namespace std; int n; struct point{double x,y;}; struct line{point a,b;}s1,s2; point sub(point a,point b) {point t;t.x=a.x-b.x;t.y=a.y-b.y;return t;} double cmul(point a,point b) {return a.x*b.y-b.x*a.y;} double turn(point p1,point p2,point p3) {return cmul(sub(p2,p1),sub(p3,p1));} bool online(line a,line b) { if(fabs(turn(a.a,a.b,b.a)*turn(a.a,a.b,b.b))>eps)return 0; if(fabs(turn(b.a,b.b,a.a)*turn(b.a,b.b,a.b))>eps)return 0; return 1; } bool par(line a,line b) { if(fabs(cmul(sub(a.a,a.b),sub(b.a,b.b)))<eps)return 1; return 0; } point cpoint(line a,line b) { double k1,k2,t; k1=cmul(sub(a.b,b.a),sub(b.b,b.a)); k2=cmul(sub(b.b,b.a),sub(a.a,b.a)); t=k1/(k1+k2); point ans; ans.x=a.b.x+(a.a.x-a.b.x)*t; ans.y=a.b.y+(a.a.y-a.b.y)*t; return ans; } int main() { printf("INTERSECTING LINES OUTPUT\n"); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&s1.a.x,&s1.a.y,&s1.b.x,&s1.b.y); scanf("%lf%lf%lf%lf",&s2.a.x,&s2.a.y,&s2.b.x,&s2.b.y); if(online(s1,s2))printf("LINE\n"); else if(par(s1,s2))printf("NONE\n"); else { point ans=cpoint(s1,s2); printf("POINT %.2lf %.2lf\n",ans.x,ans.y); } } printf("END OF OUTPUT\n"); return 0; } |
Subscribe