poj1269 Intersecting Lines
Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17149 | Accepted: 7373 |
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
Source
题目大意:n次询问,每次给两条直线,问这两条直线是共线,平行还是相交?如果相交,求交点.
分析:主要还是考察向量的各种运算.
假设a1,b1确定第一条直线,a2,b2确定第二条直线,判断这两条直线是否共线只需要判断b1-a1和a2-a1的叉乘是否为0并且b1-a1和b2-a1的叉乘是否为0.
判断平行也是利用叉乘,即判断b1-a1与b2-a2的叉乘是否为0.
如果不属于上面两种情况,就是相交了.两点确定一条直线嘛,可以用解析式将两条直线表示出来,最后解方程,一个比较巧的做法是设交点为P,那么a1-p与b1-p的叉乘为0,a2-p与b2-p的叉乘为0(共线),这样解一个二元一次方程组就可以了.
需要注意的问题:1.double是否等于0不能直接判断,要与eps做比较. 2.提交不能用g++编译器,会WA......
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const double eps = 1e-8; int n; double ansx,ansy; struct node { double x,y; } a1,b1,a2,b2; node sub(node a,node b) { node temp; temp.x = a.x - b.x; temp.y = a.y - b.y; return temp; } double det(node a,node b) { return a.x * b.y - a.y * b.x; } bool gongxian() { if (fabs(det(sub(b1,a1),sub(a2,a1))) <= eps && fabs(det(sub(b1,a1),sub(b2,a1))) <= eps) return true; return false; } bool pingxing() { if (fabs(det(sub(b1,a1),sub(b2,a2))) <= eps) return true; return false; } int main() { puts("INTERSECTING LINES OUTPUT"); scanf("%d",&n); while (n--) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a1.x,&a1.y,&b1.x,&b1.y,&a2.x,&a2.y,&b2.x,&b2.y); if (gongxian()) { puts("LINE"); continue; } if (pingxing()) { puts("NONE"); continue; } double a11 = a1.y - b1.y; double b11 = b1.x - a1.x; double c11 = a1.x*b1.y - b1.x*a1.y; double a22 = a2.y - b2.y; double b22 = b2.x - a2.x; double c22 = a2.x*b2.y - b2.x*a2.y; ansx = (c11*b22 - c22*b11) / (a22*b11 - a11*b22); ansy = (a22*c11 - a11*c22) / (a11*b22 - a22*b11); printf("POINT %.2lf %.2lf\n",ansx,ansy); } puts("END OF OUTPUT"); return 0; }