HDU1086You can Solve a Geometry Problem too(判断线段相交)
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
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.
A test case starting with 0 terminates the input and this test case is not to be processed.
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 using namespace std; 6 const int Max = 110; 7 const double eps = 0.000001; 8 struct Point 9 { 10 double x, y; 11 Point(double x = 0, double y = 0) : x(x), y(y) {} 12 }; 13 struct Line 14 { 15 Point start, End; 16 }; 17 Line line[Max]; 18 typedef Point Vector; 19 Vector operator- (Vector A, Vector B) 20 { 21 return Vector(A.x - B.x, A.y - B.y); 22 } 23 double Cross(Vector A, Vector B) 24 { 25 return A.x * B.y - A.y * B.x; 26 } 27 bool OnSegment(Point A, Point B, Point C) 28 { 29 double MinX, MaxX, MinY, MaxY; 30 if (A.x - B.x > eps) 31 { 32 MinX = B.x; 33 MaxX = A.x; 34 } 35 else 36 { 37 MinX = A.x; 38 MaxX = B.x; 39 } 40 if (A.y - B.y > eps) 41 { 42 MinY = B.y; 43 MaxY = A.y; 44 } 45 else 46 { 47 MinY = A.y; 48 MaxY = B.y; 49 } 50 // 大于等于 >= -eps 51 if (C.x - MinX >= -eps && MaxX - C.x >= -eps && C.y - MinY >= -eps && MaxY - C.y >= -eps) 52 return true; 53 return false; 54 } 55 bool solve(Line A, Line B) 56 { 57 double c1 = Cross(A.End - A.start, B.start - A.start); 58 double c2 = Cross(A.End - A.start, B.End - A.start); 59 double c3 = Cross(B.End - B.start, A.start - B.start); 60 double c4 = Cross(B.End - B.start, A.End - B.start); 61 if (c1 * c2 < 0 && c3 * c4 < 0) // && 手残写成了 || wa了好几次 62 return true; 63 if (c1 == 0 && OnSegment(A.start, A.End, B.start)) 64 return true; 65 if (c2 == 0 && OnSegment(A.start, A.End, B.End)) 66 return true; 67 if (c3 == 0 && OnSegment(B.start, B.End, A.start)) 68 return true; 69 if (c4 == 0 && OnSegment(B.start, B.End, A.End)) 70 return true; 71 return false; 72 } 73 74 int main() 75 { 76 int n; 77 while (scanf("%d", &n) != EOF && n) 78 { 79 int res = 0; 80 for (int i = 1; i <= n; i++) 81 { 82 scanf("%lf%lf%lf%lf", &line[i].start.x, &line[i].start.y, &line[i].End.x, &line[i].End.y); 83 } 84 for (int i = 1; i <= n; i++) 85 { 86 for (int j = i + 1; j <= n; j++) 87 { 88 if (solve(line[i], line[j])) 89 res++; 90 } 91 } 92 printf("%d\n", res); 93 } 94 return 0; 95 }
View Code
转载于:https://www.cnblogs.com/zhaopAC/p/5479247.html
HDU1086You can Solve a Geometry Problem too(判断线段相交)相关推荐
- hdu 1086 A - You can Solve a Geometry Problem too (线段的规范相交非规范相交)
A - You can Solve a Geometry Problem too Time Limit:1000MS Memory Limit:32768KB 64bit IO For ...
- HDU 1086 You can Solve a Geometry Problem too (判断线段交叉,线段跨立)
You can Solve a Geometry Problem too Time Limit:1000MS Memory Limit:32768KB 64bit IO Forma ...
- HDU 1086 You can Solve a Geometry Problem too
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- hdu-You can Solve a Geometry Problem too
http://acm.hdu.edu.cn/showproblem.php?pid=1086 题意:判断两两直线相交的个数 code: #include<cstdio> #include& ...
- 判断线段相交(hdu1558 Segment set 线段相交+并查集)
先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...
- 【计算几何】判断线段相交(跨立实验)
题意:有n条线段(编号为1n),按1n的顺序放在二维坐标系上(就是先放1号,再放2号--),要求输出最上面的那些线段的编号.(就是没有其他线段压在它上面的那些线段) 注意:有交点即为被压. 1.叉积 ...
- You can Solve a Geometry Problem too(线段相交问题)
Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prep ...
- 判断线段相交 + vector. erase迭代指针 的使用 poj 2653 Pick-up sticks
题目来源:http://poj.org/problem?id=2653 分析: 题意:按顺序给出一些木棍,输出在最上面的木棍标号. 用vector 存储木棍标号, 当前木棍与 vector 中的木棍 ...
- 几何基础之判断线段相交问题
1.判断两线段相交 只要判断q1,q2在线段s1s2的两侧和s1,s2在线段q1q2的两侧. q1s1q2s1>0就是在两侧. 2.矩形里有n条直线,一条直线的终点是另一条直线的起点.问矩形被分 ...
最新文章
- Datawhale两岁啦!
- linux函数怎么判断文件已更改,Linux下使用md5sum查看文件及程序是否被修改
- 55.Jump Game
- Linux 下 Jni 实现
- spring IOC 注解@Autowired
- PHP 多参数方法的重构
- 面试软件设计人员的方法,附面试题。我的面试注重实效
- 我是如何用JSP在网络上架构一个网上招标系统,以推进网站无纸化,过程电子化,管理智能化的发展
- (十二)nodejs循序渐进-高性能游戏服务器框架pomelo之创建一个游戏聊天服务器
- 进制A~Z,全字母26进制转化
- Android嵌套滑动冲突
- python基础篇–变量和简单的数据类型(下)
- AES-GCM加密算法
- linux 下xhprof的安装和使用
- 计算经纬度距离工具类
- 界面扩大缩小操作按钮_少儿编程|04.Scratch编程基本操作
- 计算机名校远程在职硕士信息汇总Online Master
- 洛谷 P1192 台阶问题
- 多任务学习综述:推荐系统多任务学习(multitask)的实战总结和常见问题(一)
- 抖音橱窗or抖音小店?这3点,新手开店必看!
热门文章
- android 并排按钮,Android TextView和Button并排,ellipsize左侧TextView
- java引用数据类型可以更改类型_Java改变引用数据类型的值
- 3 连接sybase_今日头条面试官:给我说说数据库连接池的原理?
- 关于thymeleaf配置语法运用 以及 静态资源问题总结 2021-06-08
- bootstrap验证 2021-04-21
- 英语学习app源码_无纸化英语学习APP击败%89英语学习者
- html垂直线性渐变,html5线性渐变
- java访问方法修饰词四个_java中的四个修饰词(访问权限)
- java 配置文件的路径_详解java配置文件的路径问题
- 音频放大电路_详细分析:电容器的四个典型应用电路图