Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10837 | Accepted: 4827 |
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
一定要把题意理解正确,应该是平行的时候输出“NONE”,重合的时候输出“LINE”,刚开始的时候弄反了,
一直WA还找不到问题,╮(╯▽╰)╭
<span style="font-size:18px;">#include <iostream> #include<stdio.h> #include<math.h> #include<stdlib.h> using namespace std; #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) struct point { double x,y; } p[4]; double xmult(point p1,point p2,point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } int dots_inline(point p1,point p2,point p0)//判断是否共线 { return zero(xmult(p1,p2,p0)); } double X,Y; int jiao(point a,point b,point c,point d) { double k1=(b.y-a.y)/(b.x-a.x); double k2=(d.y-c.y)/(d.x-c.x); if(k1==k2) return 0; else if(b.x==a.x&&d.x==c.x) return 0;//两直线斜率都不存在 else { if(b.x==a.x)//第一条直线斜率不存在 { X=a.x; Y=k2*(X-c.x)+c.y; } else if(d.x==c.x)//第二条直线斜率不存在 { X=c.x; Y=k1*(X-a.x)+a.y; } else//根据数学方法,有两条直线求交点 { X=(k1*a.x-a.y-k2*c.x+c.y)/(k1-k2); Y=k1*(X-a.x)+a.y; } } return 1; } int main() { int n,i,s,t,r; scanf("%d",&n); printf("INTERSECTING LINES OUTPUT\n"); for(i=0; i<n; i++) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y); s=jiao(p[0],p[1],p[2],p[3]); if(s!=0) printf("POINT %.2f %.2f\n",X,Y); else { t=dots_inline(p[0],p[1],p[2]); r=dots_inline(p[1],p[2],p[3]); if(t==1&&r==1) printf("LINE\n"); else printf("NONE\n"); } } printf("END OF OUTPUT\n"); return 0; } </span>