【计算几何】POJ 2653

题目大意:
给定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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值