题目大意:
给定n条线段,按输入依次编号为1-n,求哪些线段不与任何编号大于这条线段相交,并输出它们的编号。
这道题就是套一个计算几何模板,然后每条线段判断是否与之后的线段相交,然后输出就可以了。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
class Point
{
public:
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) { }
};
typedef Point Vector;
Vector operator + (const Vector &a, const Vector &b)
{
return Vector(a.x+b.x, a.y+b.y);
}
Vector operator - (const Point &a, const Point &b)
{
return Vector(a.x-b.x, a.y-b.y);
}
Vector operator * (const Vector &a, const double &b)
{
return Vector(a.x*b, a.y*b);
}
Vector operator / (const Vector &a, const double &b)
{
return Vector(a.x/b, a.y/b);
}
double Cross(const Vector &a, const Vector &b)
{
return a.x*b.y-a.y*b.x;
}
double Dot(const Vector &a, const Vector &b)
{
return a.x*b.x + a.y*b.y;
}
Point LineIntersection(Point p, Vector v, Point q, Vector w)
{
Vector u = p-q;
double t = Cross(w, u) / Cross(v, w);
return p+v*t;
}
bool onLine(Point a, Point b, Point p)
{
return ((p.x <= a.x && p.x >= b.x) || (p.x >= a.x && p.x <= b.x)) &&
((p.y <= a.y && p.y >= b.y) || (p.y >= a.y && p.y <= b.y));
}
Point p[100050];
Point q[100050];
Vector v[100050];
bool ans[100050];
bool check(Point p, Vector v, Point q, Vector w)
{
Point cp = LineIntersection(p, v, q, w);
return onLine(p, p+v, cp) && onLine(q, q+w, cp);
}
int main()
{
int n;
while (scanf("%d", &n) && n != 0) {
for (int i = 1; i <= n; i++) {
scanf("%lf %lf %lf %lf", &p[i].x, &p[i].y, &q[i].x, &q[i].y);
v[i] = q[i]-p[i];
}
for (int i = 1; i < n; i++) {
ans[i] = true;
for (int j = i+1; j <= n; j++)
if (check(p[i], v[i], p[j], v[j])) {
ans[i] = false;
break;
}
}
printf("Top sticks:");
for (int i = 1; i < n; i++)
if (ans[i])
printf(" %d,", i);
printf(" %d.\n", n);
}
return 0;
}