POJ_1269_Intersecting Lines_求直线交点
POJ_1269_Intersecting Lines_求直线交点
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.
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<br />
<strong>我们都知道一双独特的点在平面上定义了一条线,一条线在一个平面相交的三种方法:<br />1)没有交集,因为它们是平行的,<br />2)相交于一条线,因为他们是在另一个(即他们是相同的线),<br />3)相交于一点。在这个问题中,你将使用你的代数知识来创建一个程序来决定两条线的交点。<br />你的程序将会反复地读入四个点,在xy平面上定义两条直线,并确定直线的交点和位置。这个问题所要求的所有数字都是合理的,比如在-1000和1000之间。<br /></strong>
应该有N+2行输出。输出的第一行应该读取相交线的输出。然后,每一对平面的线代表一行输入,描述直线的交点:没有,线,或点。如果交点是一个点,那么你的程序应该输出点的x和y坐标,对小数点的两位。输出的最后一行应该是“END OF OUTPUT。
<br />先用两条直线的向量的叉积判断是否平行,然后用一个端点向另外一条直线的两个端点连线求叉积判断是否重合。<br />都不是就直接求两直线的交点,用平行四边形的面积求。<br /><br /><strong>代码:</strong><br />
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef double f2; #define eps 1e-6 f2 fabs(f2 x) {return x>0?x:-x;} struct Point { f2 x,y; Point() {} Point(f2 x_,f2 y_): x(x_),y(y_) {} Point operator + (const Point &p) const {return Point(x+p.x,y+p.y);} Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);} Point operator * (f2 rate) const {return Point(x*rate,y*rate);} void rd() {scanf("%lf%lf",&x,&y);} }; typedef Point Vector; f2 dot(const Point &p1,const Point &p2) {return p1.x*p2.x+p1.y*p2.y;} f2 cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;} struct Line { Point p; Vector v; Line() {} Line(const Point &p_,const Vector &v_): p(p_),v(v_) {} }; Point get_point(const Line &l1,const Line &l2) { Vector u=l1.p-l2.p; f2 t=cross(l2.v,u)/cross(l1.v,l2.v); return l1.p+l1.v*t; } void solve() { Point a1,a2,b1,b2; a1.rd();a2.rd();b1.rd();b2.rd(); Vector A=a1-a2,B=b1-b2; if(fabs(cross(A,B))<eps) { if(fabs(cross(a1-b1,a2-b1))<eps&&fabs(cross(a1-b2,a2-b2))<eps) puts("LINE"); else puts("NONE"); }else { Point ans=get_point(Line(a2,A),Line(b2,B)); printf("POINT %.2lf %.2lf\n",ans.x,ans.y); } } int main() { puts("INTERSECTING LINES OUTPUT"); int n; scanf("%d",&n); while(n--) { solve(); } puts("END OF OUTPUT"); }