poj1269直线交点
2016-07-18 09:15
134 查看
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
Sample Output
直线判共线:
A1 * B2 == A2 * B1 &&
B1 * C2 == B2 * C1 &&
A1 * C2 == A2 * C1
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14118 | Accepted: 6249 |
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
直线判共线:
A1 * B2 == A2 * B1 &&
B1 * C2 == B2 * C1 &&
A1 * C2 == A2 * C1
#include <iostream> #include <string> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> using namespace std; const double eps = 1e-8; double iabs(const double &k) { return k >= 0 ? k : -k;} bool cmp(const double &a, const double &b) { return iabs(a - b) < eps;} class Point{ public: double x, y; Point(){} Point(const double &a, const double &b) : x(a), y(b){} Point(const Point &k) { this->x = k.x; this->y = k.y;} Point operator - (const Point &k) { return Point(this->x - k.x, this->y - k.y);} Point operator + (const Point &k) { return Point(this->x + k.x, this->y + k.y);} void input() { scanf("%lf%lf", &x, &y);} void output() { if(cmp(x, 0)) x = 0.0; if(cmp(y, 0)) y = 0.0; printf("%.2lf %.2lf\n", x, y); } }; typedef Point Vector; class Line{ public: double a, b, c; void init(const double &x, const double &y, const double &z) { a = x; b = y; c = z;} void init(const Point &A, const Point &B) { if( cmp( A.x, B.x) ) { b = 0; a = 1.0; c = -(A.x + B.x) / 2.0; } else { b = 1.0; //a = -(B.y - A.y) / (B.x - A.x); //c = (A.x * this->a) - A.y; a = - (A.y - B.y) / (A.x - B.x); c = (A.y * B.x - B.y * A.x) / (A.x - B.x); } } Point crossPoint( const Line &k) { double d = (this->a * k.b - this->b * k.a); double x = -(this->c * k.b - this->b * k.c) / d; double y = (this->c * k.a - this->a * k.c) / d; return Point(x, y); } int operator == (const Line &k) { //if( this->b / this->a == k.b / k.a) if( cmp(0, this->a * k.b - this->b * k.a)) { if( cmp(this->a * k.b, this->b * k.a) && cmp(this->b * k.c, this->c * k.b) && cmp( this->a * k.c, this->c * k.a)) return 1; return 2; } return 0; } void output() { printf("%g X + %g y + %g = 0\n", a, b, c); } }; void solve() { Point A, B, C, D, E, ans; Vector CB, CA; Line l1, l2; int T; cin >> T; puts("INTERSECTING LINES OUTPUT"); while(T--) { A.input(); B.input(); C.input(); D.input(); l1.init(A, B); l2.init(C, D); //l1.output(); //l2.output(); int k = (l1 == l2); if(k == 1) puts("LINE"); else if(k == 2) puts("NONE"); else { printf("POINT "); ans = l1.crossPoint( l2 ); ans.output(); } } puts("END OF OUTPUT"); } int main() { solve(); return 0; }
相关文章推荐
- 基于 CoreText 实现的高性能 UITableView
- first day
- 那些做 4000 Android开发必须知道的ADB命令
- 搜索引擎研究---网络蜘蛛程序算法相关资料 Part VI (共5部分)
- int,long,long long类型的数值范围
- 解决 Sublime text3 中文显示乱码问题【亲测可用】
- 本来计划送老婆儿子去公主岭
- openfire client聊天消息交互和存储离线(在线)消息记录策略
- 搜索引擎研究---网络蜘蛛程序算法相关资料 Part III (共5部分)
- 从0开始学习 GitHub 系列之「Git速成」
- 成功输出消息后的采购订单不能取消审核
- Android:LinearLayout布局中Layout_weight的深刻理解
- [学习笔记]javascript的语法和关键字
- 生产订单收货数量与物料凭证计算总数量不一致
- 搜索引擎研究---网络蜘蛛程序算法相关资料 Part II (共5部分)
- Android模版化编程系列(二)
- oracle之order by
- 【安卓面试题】简述一下Android系统的架构?
- svg
- 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)