1269 Intersecting Lines
来源:互联网 发布:电影购票系统java 编辑:程序博客网 时间:2024/06/09 14:47
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
Source
Mid-Atlantic 1996
/*求出直线方程y=slope*x+b,比较a和b的值即可。注意讨论斜率不存在的情况*/#include<iostream>using namespace std;int main(){ int N; cin >> N; cout << "INTERSECTING LINES OUTPUT" << endl; while (N--) { int x1, y1, x2, y2, x3, y3, x4, y4; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; double x, y; double slope1, slope2, b1, b2; /*斜率不存在*/ if (x2 - x1 == 0 && x4 - x3 == 0) { if (x1 == x3) cout << "LINE" << endl; else cout << "NONE" << endl; } else if (x2 - x1 == 0) { slope2 = 1.0*(y4 - y3) / (x4 - x3); b2 = y3 - x3*slope2; x = x1; y = slope2*x + b2; cout << "POINT "; printf("%.2f %.2f\n", x, y); } else if (x4 - x3 == 0) { slope1 = 1.0*(y2 - y1) / (x2 - x1); b1 = y1 - x1*slope1; x = x2; y = slope1*x + b1; cout << "POINT "; printf("%.2f %.2f\n", x, y); } /*斜率存在*/ else { slope1 = 1.0*(y2 - y1) / (x2 - x1); slope2 = 1.0*(y4 - y3) / (x4 - x3); b1 = y1 - x1*slope1; b2 = y3 - x3*slope2; if (slope1 == slope2&&b1 == b2) cout << "LINE" << endl; else if (slope1 == slope2) cout << "NONE" << endl; else { x = (b2 - b1) / (slope1 - slope2); y = slope1*x + b1; cout << "POINT "; printf("%.2f %.2f\n", x, y); } } } cout << "END OF OUTPUT" << endl; return 0;}
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- 有n个矩形,每个矩形可以用a,b来描述,表示长和宽。矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度)。例如(1,5)可以嵌套在(6,2)内,但
- android 日历自定义控件 ,包含 阴历 和 阳历
- Android 混合音频,评价算法
- JavaSE 注解和多线程
- Android静默安装 root提权方式
- 1269 Intersecting Lines
- TCP协议相关
- hdu 6165(dfs or bfs or tarjan+topsort)
- 数组简单排序
- 【剑指offer】面试题 49:丑数
- lua 数组的索引可以是负数
- 八皇后
- Spring之IOC
- HDU6162 Ch’s gift【LCA】