POJ1269 Intersecting Lines
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
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
数学 几何
判断两条直线的位置关系
叉积的花式应用233
方向相同的两向量叉积为0,据此可以判断两条直线平行或是重合
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const double eps=1e-8; 10 const int mxn=100010; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 14 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 struct Point{ 18 double x,y; 19 Point operator + (Point rhs){return (Point){x+rhs.x,y+rhs.y};} 20 Point operator - (Point rhs){return (Point){x-rhs.x,y-rhs.y};} 21 Point operator * (double len){return (Point){x*len,y*len};} 22 }p[5],l[3]; 23 inline double Cross(Point a,Point b){ 24 return a.x*b.y-a.y*b.x; 25 } 26 Point intersec(Point a,Point v,Point b,Point w){ 27 Point v1=a-b; 28 double t=Cross(w,v1)/Cross(v,w); 29 return a+v*t; 30 } 31 int main(){ 32 int i,j; 33 int T=read(); 34 printf("INTERSECTING LINES OUTPUT\n"); 35 while(T--){ 36 p[1].x=read();p[1].y=read(); 37 p[2].x=read();p[2].y=read(); 38 p[3].x=read();p[3].y=read(); 39 p[4].x=read();p[4].y=read(); 40 l[1]=p[2]-p[1]; 41 l[2]=p[4]-p[3]; 42 if(abs(Cross(l[1],p[3]-p[1])<eps) && abs(Cross(l[1],p[4]-p[1]))<eps){ 43 printf("LINE\n"); 44 continue; 45 } 46 if(Cross(l[1],l[2])==0){ 47 printf("NONE\n"); 48 continue; 49 } 50 Point ans=intersec(p[1],l[1],p[3],l[2]); 51 printf("POINT %.2f %.2f\n",ans.x,ans.y); 52 } 53 printf("END OF OUTPUT\n"); 54 return 0; 55 }
本文为博主原创文章,转载请注明出处。