HDU1086You can Solve a Geometry Problem too(判断线段相交)

时间:2023-03-09 03:51:45
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

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个线段,求线段相交的个数
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = ;
const double eps = 0.000001;
struct Point
{
double x, y;
Point(double x = , double y = ) : 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 < && c3 * c4 < ) // && 手残写成了 || wa了好几次
return true;
if (c1 == && OnSegment(A.start, A.End, B.start))
return true;
if (c2 == && OnSegment(A.start, A.End, B.End))
return true;
if (c3 == && OnSegment(B.start, B.End, A.start))
return true;
if (c4 == && OnSegment(B.start, B.End, A.End))
return true;
return false;
} int main()
{
int n;
while (scanf("%d", &n) != EOF && n)
{
int res = ;
for (int i = ; 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 = ; i <= n; i++)
{
for (int j = i + ; j <= n; j++)
{
if (solve(line[i], line[j]))
res++;
}
}
printf("%d\n", res);
}
return ;
}