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
题目大意,求给定两条线段的位置关系。
这是计算几何的基本操作,也没什么好说的吧(=o=),直接上板。
#include<cmath> #include<cstring> #include<cstdio> #include<algorithm> #define Vec point using namespace std; ; int n; struct point{ double x,y; void read(){scanf("%lf%lf",&x,&y);} void write(){printf("POINT %.2lf %.2lf\n",x,y);} }seg1a,seg1b,seg2a,seg2b,ans; :x>eps;} Vec operator + (point u,Vec v){ Vec ret; ret.x=u.x+v.x,ret.y=u.y+v.y; return ret; } Vec operator - (point u,point v){ Vec ret; ret.x=u.x-v.x,ret.y=u.y-v.y; return ret; } Vec operator * (Vec u,double v){ Vec ret; ret.x=u.x*v,ret.y=u.y*v; return ret; } Vec operator / (Vec u,double v){ Vec ret; ret.x=u.x/v,ret.y=u.y/v; return ret; } double cross(Vec u,Vec v){return u.x*v.y-u.y*v.x;} struct line{ double A,B,C; }seg1,seg2; line maker(point u,point v){ line ret; ret.A=v.y-u.y; ret.B=u.x-v.x; ret.C=v.x*u.y-u.x*v.y; return ret; } int intersect(line u,line v,point &ret){ ){ &&fabso(u.C*v.B-v.C*u.B)==; } ret.x=(u.B*v.C-v.B*u.C)/(u.A*v.B-v.A*u.B); ret.y=(v.A*u.C-u.A*v.C)/(u.A*v.B-v.A*u.B); ; } int main(){ scanf("%d",&n); puts("INTERSECTING LINES OUTPUT"); ; i<=n; i++){ seg1a.read(),seg1b.read(),seg2a.read(),seg2b.read(); seg1=maker(seg1a,seg1b); seg2=maker(seg2a,seg2b); ) puts("LINE");else ) puts("NONE");else ans.write(); } puts("END OF OUTPUT"); ; }