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

Problem Description
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.

Input
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.
Output
For each case, print the number of intersections, and one line one case.
Sample Input
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
Sample Output
1 3
给出N个线段,求线段相交的个数

 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(判断线段相交)相关推荐

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. hdu-You can Solve a Geometry Problem too

    http://acm.hdu.edu.cn/showproblem.php?pid=1086 题意:判断两两直线相交的个数 code: #include<cstdio> #include& ...

  5. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  6. 【计算几何】判断线段相交(跨立实验)

    题意:有n条线段(编号为1n),按1n的顺序放在二维坐标系上(就是先放1号,再放2号--),要求输出最上面的那些线段的编号.(就是没有其他线段压在它上面的那些线段) 注意:有交点即为被压. 1.叉积 ...

  7. You can Solve a Geometry Problem too(线段相交问题)

     Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prep ...

  8. 判断线段相交 + vector. erase迭代指针 的使用 poj 2653 Pick-up sticks

    题目来源:http://poj.org/problem?id=2653 分析: 题意:按顺序给出一些木棍,输出在最上面的木棍标号. 用vector 存储木棍标号, 当前木棍与 vector 中的木棍 ...

  9. 几何基础之判断线段相交问题

    1.判断两线段相交 只要判断q1,q2在线段s1s2的两侧和s1,s2在线段q1q2的两侧. q1s1q2s1>0就是在两侧. 2.矩形里有n条直线,一条直线的终点是另一条直线的起点.问矩形被分 ...

最新文章

  1. Datawhale两岁啦!
  2. linux函数怎么判断文件已更改,Linux下使用md5sum查看文件及程序是否被修改
  3. 55.Jump Game
  4. Linux 下 Jni 实现
  5. spring IOC 注解@Autowired
  6. PHP 多参数方法的重构
  7. 面试软件设计人员的方法,附面试题。我的面试注重实效
  8. 我是如何用JSP在网络上架构一个网上招标系统,以推进网站无纸化,过程电子化,管理智能化的发展
  9. (十二)nodejs循序渐进-高性能游戏服务器框架pomelo之创建一个游戏聊天服务器
  10. 进制A~Z,全字母26进制转化
  11. Android嵌套滑动冲突
  12. python基础篇–变量和简单的数据类型(下)
  13. AES-GCM加密算法
  14. linux 下xhprof的安装和使用
  15. 计算经纬度距离工具类
  16. 界面扩大缩小操作按钮_少儿编程|04.Scratch编程基本操作
  17. 计算机名校远程在职硕士信息汇总Online Master
  18. 洛谷 P1192 台阶问题
  19. 多任务学习综述:推荐系统多任务学习(multitask)的实战总结和常见问题(一)
  20. 抖音橱窗or抖音小店?这3点,新手开店必看!

热门文章

  1. android 并排按钮,Android TextView和Button并排,ellipsize左侧TextView
  2. java引用数据类型可以更改类型_Java改变引用数据类型的值
  3. 3 连接sybase_今日头条面试官:给我说说数据库连接池的原理?
  4. 关于thymeleaf配置语法运用 以及 静态资源问题总结 2021-06-08
  5. bootstrap验证 2021-04-21
  6. 英语学习app源码_无纸化英语学习APP击败%89英语学习者
  7. html垂直线性渐变,html5线性渐变
  8. java访问方法修饰词四个_java中的四个修饰词(访问权限)
  9. java 配置文件的路径_详解java配置文件的路径问题
  10. 音频放大电路_详细分析:电容器的四个典型应用电路图