您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: