HDU1086You can Solve a Geometry Problem too(判断线段相交)
2016-05-10 20:19
323 查看
You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9596 Accepted Submission(s): 4725
[align=left]Problem Description[/align]
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
[align=left]Input[/align]
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.
[align=left]Output[/align]
For each case, print the number of intersections, and one line one case.
[align=left]Sample Input[/align]
2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0
[align=left]Sample Output[/align]
1
3
给出N个线段,求线段相交的个数
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int Max = 110; const double eps = 0.000001; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} }; struct Line { Point start, End; }; Line line[Max]; typedef Point Vector; Vector operator- (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } bool OnSegment(Point A, Point B, Point C) { double MinX, MaxX, MinY, MaxY; if (A.x - B.x > eps) { MinX = B.x; MaxX = A.x; } else { MinX = A.x; MaxX = B.x; } if (A.y - B.y > eps) { MinY = B.y; MaxY = A.y; } else { MinY = A.y; MaxY = B.y; } // 大于等于 >= -eps if (C.x - MinX >= -eps && MaxX - C.x >= -eps && C.y - MinY >= -eps && MaxY - C.y >= -eps) return true; return false; } bool solve(Line A, Line B) { double c1 = Cross(A.End - A.start, B.start - A.start); double c2 = Cross(A.End - A.start, B.End - A.start); double c3 = Cross(B.End - B.start, A.start - B.start); double c4 = Cross(B.End - B.start, A.End - B.start); if (c1 * c2 < 0 && c3 * c4 < 0) // && 手残写成了 || wa了好几次 return true; if (c1 == 0 && OnSegment(A.start, A.End, B.start)) return true; if (c2 == 0 && OnSegment(A.start, A.End, B.End)) return true; if (c3 == 0 && OnSegment(B.start, B.End, A.start)) return true; if (c4 == 0 && OnSegment(B.start, B.End, A.End)) return true; return false; } int main() { int n; while (scanf("%d", &n) != EOF && n) { int res = 0; for (int i = 1; i <= n; i++) { scanf("%lf%lf%lf%lf", &line[i].start.x, &line[i].start.y, &line[i].End.x, &line[i].End.y); } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (solve(line[i], line[j])) res++; } } printf("%d\n", res); } return 0; }
View Code
相关文章推荐
- java文件上传和下载
- linux下终端,vim显示中文为乱码
- Windows使用免费版Kiwisyslog搭建日志服务器
- 内存的动态开辟
- SQL中数据库的基本操作
- 地铁译:Spark for python developers --- 搭建Spark虚拟环境3
- CentOS 7中防火墙 firewall-cmd命令
- tornado 异步调用中的 StackContext
- Linux-1.体系结构-初级(查看内核,操作系统)
- 聊聊Condition
- ruby 语法规则(syntax rules)
- iOS 开启文件共享权限
- cojs 简单的数位DP 题解报告
- 聊聊Condition
- iOS UIViewContentMode各类型效果
- 水电安装知识大全
- JQuery_ JQuery选择器 JQuery判断相应的节点是否存在
- PAT中1001.害死人不偿命的(3n+1)猜想 (15)
- L - Demacia of the Ancients——ZOJ
- ORACLE用SYS登录报ORA-28009:connection as SYS should be as SYSDBA OR SYSOPER解决方法