Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8637 | Accepted: 3915 |
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
Source
Mid-Atlantic 1996
![](http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif)
![](http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif)
1 #include2 #include 3 #include 4 using namespace std; 5 #define eps 1e-8 6 #define oo 100000000 7 #define pi acos(-1) 8 struct point 9 { 10 double x,y; 11 point(double _x = 0.0,double _y = 0.0) 12 { 13 x =_x; 14 y =_y; 15 } 16 point operator -(const point &b)const 17 { 18 return point(x - b.x, y - b.y); 19 } 20 point operator +(const point &b)const 21 { 22 return point(x +b.x, y + b.y); 23 } 24 double operator ^(const point &b)const 25 { 26 return x*b.y - y*b.x; 27 } 28 double operator *(const point &b)const 29 { 30 return x*b.x + y*b.y; 31 } 32 }p[10]; 33 34 double dis(point a,point b)//两点之间的距离 35 { 36 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 37 } 38 39 int dcmp(double a)//判断一个double型的符号 40 { 41 if(fabs(a) return 0; 42 if(a>0)return 1; 43 else return -1; 44 } 45 46 int isxiangjiao(point a,point b,point c,point d)//判断直线相交,重合,平行!!! 47 { 48 point aa,bb,cc,dd; 49 aa=b-a; 50 bb=d-c; 51 if(dcmp(aa^bb)!=0)return 1;//相交 52 else 53 { 54 aa=a-d; 55 bb=b-c; 56 cc=a-c; 57 dd=b-d; 58 if(dcmp(aa^bb)!=0||dcmp(cc^dd)!=0)return 2;//平行 59 else return 3;//重合 60 } 61 } 62 63 point getjiaodian(point p,point v,point q,point w)//参数方程,v,w都为方向向量,p,q,为两直线上的点,求交点 64 { 65 point u; 66 u=p-q; 67 double t=(w^u)/(v^w); 68 v.x=t*v.x;v.y=t*v.y; 69 return p+v; 70 } 71 72 int main() 73 { 74 int T,i,j; 75 scanf("%d",&T); 76 printf("INTERSECTING LINES OUTPUT\n"); 77 while(T--) 78 { 79 for(i=1;i<=4;i++)scanf("%lf%lf",&p[i].x,&p[i].y); 80 81 if(isxiangjiao(p[1],p[2],p[3],p[4])==1) 82 { 83 point ans,v,w,q; 84 v=p[2]-p[1]; 85 w=p[4]-p[3]; 86 ans=getjiaodian(p[1],v,p[3],w); 87 printf("POINT %.2f %.2f\n",ans.x,ans.y); 88 } 89 90 if(isxiangjiao(p[1],p[2],p[3],p[4])==2)printf("NONE\n");//平行 91 92 if(isxiangjiao(p[1],p[2],p[3],p[4])==3)printf("LINE\n");//重合 93 } 94 printf("END OF OUTPUT\n"); 95 return 0; 96 }