Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10934 | Accepted: 4911 |
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
简单几何
吐=槽:。。。。。。。。。。。。。。这是我搞的第二发计算几何题,今天一整天只做了这一个题,下午提交了8遍,都是WA,我的心真痛了,智商真不够用了,晚上cjx学长解答,终于找出了问题,这道题是求两直线间的问题,而我一直当成线段做的。 而且还做的那么麻烦。。。。。其实做法只这样的,两直线间的关系只有两种,要么相交(输出交点 point (x0, y0)),要么平行。其中平行又分两种情况,共线平行(输出 line )和不共线平行(输出 none)。 。。就这么简单。。。。。。。。。。。
代码如下:
#include<stdio.h> #include<math.h> #define eps 1e-8 struct point { double x1, y1, x2, y2, x3, y3, x4, y4; } p[200]; bool Xmul(point p)//判差乘 { if( (p.x3-p.x1)*(p.y2-p.y1)-(p.y3-p.y1)*(p.x2-p.x1) == 0) return true; return false; } void intersection(point p)//求交点 { point ret=p; double t=((p.x1-p.x3)*(p.y3-p.y4)-(p.y1-p.y3)*(p.x3-p.x4)) /((p.x1-p.x2)*(p.y3-p.y4)-(p.y1-p.y2)*(p.x3-p.x4)); ret.x1+=(p.x2-p.x1)*t; ret.y1+=(p.y2-p.y1)*t; printf("POINT %.2lf %.2lf ", ret.x1, ret.y1); } void Judge(point p) { double u, v, w; u = (p.x2-p.x1)*(p.y4-p.y3)-(p.x4-p.x3)*(p.y2-p.y1);//交叉相乘,判平行 if(fabs(u)== 0) { if(Xmul(p)) printf("LINE "); //判断是否平行或共线 else printf("NONE "); return; } else { intersection(p); return; } } int main() { int n, i; scanf("%d", &n); printf("INTERSECTING LINES OUTPUT "); for(i=0; i<n; i++) { scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &p[i].x1, &p[i].y1, &p[i].x2, &p[i].y2, &p[i].x3, &p[i].y3, &p[i].x4, &p[i].y4); Judge(p[i]); } printf("END OF OUTPUT "); return 0; }