You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (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.
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.
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
分析:
线段判交,分别判断在端点处相交于不是在端点处相交
代码:
#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;
}