Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10967 | Accepted: 4930 |
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.
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
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
题意:
给两条线,求他们之间的关系。关系有3种:相交、共线、平行。
思路:
模拟手算就行了,注意直线可能没有斜率,用G++提交时用%.2f而不能用%.2lf,否则判错。
代码:
1 #include <iostream> 2 #include <string> 3 #include <map> 4 #include <stdio.h> 5 #include <math.h> 6 using namespace std; 7 #define pi acos(-1) 8 9 10 main() 11 { 12 int t, t1; 13 double x1, y1, x2, y2, x3, y3, x4, y4; 14 double k1, b1, k2, b2, x, y; 15 cin>>t; 16 t1=t; 17 while(t--){ 18 if(t1==t+1){ 19 printf("INTERSECTING LINES OUTPUT\n"); 20 } 21 scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); 22 if(x1==x2&&x3==x4){ //当两条线都没有斜率时 23 if(x1==x3) printf("LINE\n"); 24 else printf("NONE\n"); 25 } 26 else if(x1==x2){ //第一条线没斜率 27 k1=(y4-y3)/(x4-x3); 28 b1=y4-k1*x4; 29 printf("POINT %.2f %.2f\n",x1,k1*x1+b1); 30 } 31 else if(x3==x4){ //第二条线没斜率 32 k1=(y2-y1)/(x2-x1); 33 b1=y2-k1*x1; 34 printf("POINT %.2f %.2f\n",x3,k1*x3+b1); 35 } 36 else{ //都有斜率 37 k1=(y2-y1)/(x2-x1); 38 k2=(y4-y3)/(x4-x3); 39 b1=y2-k1*x2; 40 b2=y4-k2*x4; 41 if(k1==k2&&b1==b2){ 42 printf("LINE\n"); 43 } 44 else if(k1==k2){ 45 printf("NONE\n"); 46 } 47 else{ 48 x=(b1-b2)/(k2-k1); 49 y=k1*x+b1; 50 printf("POINT %.2f %.2f\n",x,y); 51 } 52 } 53 if(t==0) printf("END OF OUTPUT\n"); 54 } 55 }
POJ 1269 两线的关系