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
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
ps:终于给水出来了,自己一个人只能用最笨的方法了,学长给的浮点函数完全不知道什么意思,一上午。开始写的代码自己看了都头晕,后来看到别人把些乱七八糟的数都定义了,我也改了改,现在起码能看了。
题意:给出n组测试数据,每组有两条直线(由四个点构成的),若是两条直线相交,则输出交点,若是共线输出LINE,若是平行输出NONE;
思路 设直线为y=kx+b; 所以第一条直线可以表示为y=(y2-y1)*x/(x2-x1)+(x2*y1-x1*y2)/(x2-x1);第二条直线可以表示为y=(y4-y3)*x/(x4-x3)+(x4*y3-x3*y4)/(x4-x3);然后分情况讨论;
斜率不存在:
(1)当一条斜率存在,另一条斜率存在;
(2)另一条斜率存在,这一条不存在;
(3)两条斜率都不存在;
斜率存在
(4)两条直线共线;
(5)两条直线平行但不共线;
(6)存在交点;
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int main() { int n; double x1,y1,x2,y2,x3,y3,x4,y4; scanf("%d",&n); puts("INTERSECTING LINES OUTPUT"); while(n--) { scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); double a=x2-x1; double b=y2-y1; double c=x4-x3; double d=y4-y3; double x,y; double k1=b/a; double b1=y1-k1*x1; double k2=d/c; double b2=y3-k2*x3; if(x1==x2||x3==x4) { if(x1==x2&&x3==x4)//两条斜率都不存在,所以他们的方向是垂直于x轴的,所以只需要看x轴的坐标是否相等 { if(x1==x3)//相等则共线 printf("LINE\n"); else printf("NONE\n");//不相等则平行 } else if(x1==x2)//第一条斜率不存在,第二条存在 { x=x1; y=(d*x+x4*y3-x3*y4)/c; printf("POINT %.2lf %.2lf\n",x,y); } else//第一条斜率存在,第二条不存在; { x=x3; y=(b*x+x2*y1-x1*y2)/a; printf("POINT %.2lf %.2lf\n",x,y); } continue; } //下面是斜率存在的情况 if(a*d==b*c)//斜率相等 { if(y1==k2*x1+b2)//如果这条直线的点也在另一条直线上,证明重合 printf("LINE\n"); else printf("NONE\n");//否则平行; } else//有交点的情况 { x=(b1-b2)/(k2-k1); y=k2*x+b2; printf("POINT %.2lf %.2lf\n",x,y); } } puts("END OF OUTPUT"); return 0; }