HDU 1086 You can Solve a Geometry Problem too
2015-12-04 21:18
375 查看
You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768K (Java/Others)
Total Submission(s): 9205 Accepted Submission(s): 4508
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
Author
lcy
Recommend
We have carefully selected several similar problems for you: 1115 1392 2108 2150 1348
题目链接
分析:
线段判交,分别判断在端点处相交于不是在端点处相交
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct Point //点
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {} //构造函数,方便代码编写
};
struct Segment //线段
{
Point p1, p2;
} S[110];
typedef Point Vector; //Vector 是 Point 的别名
//点-点=向量
Vector operator - (Point A, Point B)
{
return Vector(A.x - B.x, A.y - B.y);
}
const double eps = 1e-10;
//三态函数,减少精度问题
int dcmp(double x)
{
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
//两向量的叉积
double Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
}
//判定线段是否“规范相交”
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
//判断是否在端点处相交
bool OnSegment(Point a1, Point a2, Point b1, Point b2)
{
return dcmp(Cross(a1 - b1, a2 - b1)) == 0
|| dcmp(Cross(a1 - b2, a2 - b2)) == 0
|| dcmp(Cross(b1 - a1, b2 - a1)) == 0
|| dcmp(Cross(b1 - a2, b2 - a2)) == 0;
return 0;
}
int main()
{
int n;
while (~scanf("%d", &n) && n != 0)
{
for (int i = 1; i <= n; i++)
scanf("%lf%lf%lf%lf", &S[i].p1.x, &S[i].p1.y, &S[i].p2.x, &S[i].p2.y);
int cnt = 0;
for (int i = 1; i < n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (SegmentProperIntersection(S[i].p1, S[i].p2, S[j].p1, S[j].p2)
|| OnSegment(S[i].p1, S[i].p2, S[j].p1, S[j].p2))
{
cnt++;
}
}
}
printf("%d\n", cnt);
}
return 0;
}
相关文章推荐
- E-栈--括号匹配
- project euler 38
- 53,类方法
- 数组循环语句练习
- Java [Leetcode 39]Combination Sum
- LibSVM C/C++
- project euler 37
- LibSVM C/C++
- hud 2099
- PHPCMS几个有用的全局函数
- hdu1176
- java获取系统时间比实际时间少了8个小时的解决方法
- NYOJ 119士兵杀敌(三)【RMQ裸题】
- EF--.net
- [leetcode] Ugly Number II
- otsu算法---matlab实现,和一种改进算法
- 解压版的Tomcat基本配置和安装
- 双向循环链表的实现
- 大数据时代:生活、工作与思维的大变革(上)
- 复杂链表的复制(C++)