跳到正文

剪贴板

剪贴板 vquekai8

原文
剪贴板信息

剪贴板信息

收录
更新
C++
// #define USE_DOUBLE
#define USE_THETA
#define USE_FLOAT
namespace Geometry {
// a 在 b 右侧,cross(a, b) > 0
using T = long double;
struct Point {
    T x, y;
};
struct Line {
    Point a, b;
};
#ifdef USE_FLOAT
constexpr T eps = 1e-8;
inline int sign(T x) { return x < -eps ? -1 : (x > eps ? 1 : 0); }
inline T absx(T x) { return x * sign(x); }
#else
constexpr T Z = 0;
inline int sign(T x) { return x < Z ? -1 : (x > Z ? 1 : 0); }
inline T absx(T x) { return x * sign(x); }
#endif
#ifdef USE_DOUBLE
#define cosl cos
#define sinl sin
#define sqrtl sqrt
#endif

using Vector = Point;
inline Vector operator+(Vector a, Vector b) { return {a.x + b.x, a.y + b.y}; }
inline Vector operator-(Vector a) { return {-a.x, -a.y}; }
inline Vector operator-(Vector a, Vector b) { return {a.x - b.x, a.y - b.y}; }
inline Vector operator*(Vector a, T b) { return {a.x * b, a.y * b}; }
inline Vector operator/(Vector a, T b) { return {a.x / b, a.y / b}; }
inline bool operator==(Vector a, Vector b) {
    return !sign(a.x - b.x) && !sign(a.y - b.y);
}
inline bool operator!=(Vector a, Vector b) { return !(a == b); }
inline T dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
inline T cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
#ifdef USE_FLOAT
inline T len(Vector x) { return sqrtl(dot(x, x)); }
inline T displ(Point x, Line y) {
    if (y.a == y.b) return len(x - y.a);
    Vector a = x - y.a, b = x - y.b, c = y.b - y.a;
    if (sign(dot(a, c)) < 0) return len(a);
    if (sign(dot(b, c)) > 0) return len(b);
    return absx(cross(a, c) / len(c));
}
#endif
#ifdef USE_THETA
inline Vector rotate(Vector a, T b) {  // 顺时针
    T x = cosl(b), y = sinl(b);
    return {a.x * x + a.y * y, a.y * x - a.x * y};
}
inline Vector rotatewith(Vector a, Vector b, T c) {  // a 绕 b 转 c
    return rotate(a - b, c) + b;
}
#endif

inline bool operator==(Line a, Line b) {
    return (a.a == b.a && a.b == b.b) || (a.a == b.b && a.b == b.a);
}
inline bool operator!=(Line a, Line b) { return !(a == b); }

inline bool pol(Point x, Line y) {
    return !sign(cross(x - y.a, y.b - y.a)) && sign(dot(x - y.a, x - y.b)) <= 0;
}

inline bool polx(Point x, Line y) { return !sign(cross(x - y.a, y.b - y.a)); }
inline Point foot(Point x, Line y) {
    Vector a = x - y.a, b = x - y.b, c = y.b - y.a;
    T len1 = dot(a, c), len2 = -dot(b, c);
    return y.a + c * len1 / (len1 + len2);
}
inline Vector pdispl(Point x, Line y) {
    if (y.a == y.b) return y.a;
    Vector a = x - y.a, b = x - y.b, c = y.b - y.a;
    if (sign(dot(a, c)) < 0) return y.a;
    if (sign(dot(b, c)) > 0) return y.b;
    return foot(x, y);
}
inline Point symmetrypl(Point x, Line y) { return x + (foot(x, y) - x) * 2; }
inline Point crosslxlx(Line x, Line y) {
    Vector a = x.b - x.a, b = y.b - y.a, c = x.a - y.a;
    return x.a + a * (cross(b, c) / cross(a, b));
}
inline bool ifparall(Line x, Line y) {
    return !sign(cross(x.b - x.a, y.b - y.a));
}
inline bool ifcrosslxlx(Line a, Line b) { return !ifparall(a, b); }
inline bool ifcrosslxl(Line a, Line b) {
    if (ifparall(a, b)) return 0;
    return pol(crosslxlx(a, b), b);
}
inline bool ifcrossll(Line a, Line b) {
    if (ifparall(a, b)) return 0;
    Point c = crosslxlx(a, b);
    return pol(c, a) && pol(c, b);
}
using Poly = vector<Vector>;
inline int judgePIP(const Poly &x, Point a) {
    if (x.size() == 1) return x[0] == a ? 2 : 0;
    int cnt = 0;
    T tmp;
    for (int i = 0; i < x.size(); i++) {
        int j = (i + 1) == x.size() ? 0 : i + 1;
        if (pol(a, {x[i], x[j]})) return 2;
        if (!sign(x[i].y - x[j].y)) continue;
        if (a.y >= min(x[i].y, x[j].y) && a.y < max(x[i].y, x[j].y))
            tmp =
                x[i].x + (a.y - x[i].y) / (x[j].y - x[i].y) * (x[j].x - x[i].x),
            cnt += sign(tmp - a.x) > 0;
    }
    return cnt & 1;
}
inline bool judge(Point a, Point L, Point R) {
    return sign(cross(L - a, R - a)) > 0;
}
inline int rip_rev(const Poly &x, Point a) {
    if (x.size() == 1) return x[0] == a ? 2 : 0;
    if (judge(x[0], a, x[1]) || judge(x[0], x.back(), a)) return 0;
    if (pol(a, {x[0], x[1]}) || pol(a, {x[0], x.back()})) return 2;
    int l = 1, r = x.size() - 2;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (judge(x[0], x[mid], a))
            l = mid;
        else
            r = mid - 1;
    }
    if (judge(x[l], a, x[l + 1])) return 0;
    if (pol(a, {x[l], x[l + 1]})) return 2;
    return 1;
}
inline int rip(const Poly &x, Point a) {
    if (x.size() == 1) return 0;
    if (judge(x.back(), a, x[x.size() - 2]) || judge(x.back(), x[0], a))
        return 0;
    if (pol(a, {x.back(), x[x.size() - 2]}) || pol(a, {x.back(), x[0]}))
        return 2;

    int l = 1, r = x.size() - 2, p = x.size() - 1;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (judge(x.back(), x[p - mid], a))
            l = mid;
        else
            r = mid - 1;
    }
    if (judge(x[p - l], a, x[p - l - 1])) return 0;
    if (pol(a, {x[p - l], x[p - l - 1]})) return 2;
    return 1;
}
inline bool judgepp(const Poly &a, const Poly &b) {
    if (rip(a, b[0]) || rip(b, a[0])) return 0;
    for (int i = 0; i < a.size(); i++) {
        int ni = (i + 1 == a.size()) ? 0 : i + 1;
        for (int j = 0; j < b.size(); j++) {
            int nj = (j + 1 == b.size()) ? 0 : j + 1;
            if (ifcrossll({a[i], a[ni]}, {b[j], b[nj]})) return 0;
        }
    }
    return 1;
}
inline T area(const Poly &x) {
    T ans = 0;
    for (int i = 0; i + 1 < x.size(); i++) ans += cross(x[i], x[i + 1]);
    ans += cross(x.back(), x[0]);
    return absx(ans / 2);
}
#ifdef USE_FLOAT
inline T circum(const Poly &x) {
    T ans = 0;
    for (int i = 0; i + 1 < x.size(); i++) ans += len(x[i + 1] - x[i]);
    return ans + len(x.back() - x[0]);
}
#endif
inline void fsort(Poly &x) {
    int mn = 0;
    for (int i = 1; i < x.size(); i++)
        if (sign(x[i].y - x[mn].y) < 0 ||
            (!sign(x[i].y - x[mn].y) && sign(x[i].x - x[mn].x) < 0))
            mn = i;
    swap(x[mn], x[0]);
    sort(x.begin() + 1, x.end(), [&](Point a, Point b) {
        T c = cross(a - x[0], b - x[0]);
        if (sign(c)) return sign(c) == -1;
        return sign(a.y - b.y) ? sign(a.y - b.y) == -1 : sign(a.x - b.x) == -1;
    });
}
inline Poly convexhull(Poly x) {
    Poly res;
    fsort(x), res.push_back(x[0]);
    for (int i = 1; i < x.size(); i++) {
        while (res.size() >= 2) {
            Vector a = res.back(), b = res[res.size() - 2];
            if (sign(cross(a - b, x[i] - b)) >= 0)
                res.pop_back();
            else
                break;
        }
        res.push_back(x[i]);
    }
    return res;
}
inline Poly operator+(Poly a, const Poly &b) {
    Poly s1, s2, res;
    s1.resize(a.size()), s2.resize(b.size());
    for (int i = 0; i + 1 < a.size(); i++) s1[i] = a[i + 1] - a[i];
    for (int i = 0; i + 1 < b.size(); i++) s2[i] = b[i + 1] - b[i];
    s1.back() = a[0] - a.back(), s2.back() = b[0] - b.back(),
    res.push_back(a[0] + b[0]);
    int pa = 0, pb = 0;
    while (pa < a.size() && pb < b.size()) {
        if (sign(cross(s1[pa], s2[pb])) < 0)
            res.push_back(res.back() + s1[pa++]);
        else
            res.push_back(res.back() + s2[pb++]);
    }
    while (pa < a.size()) res.push_back(res.back() + s1[pa++]);
    while (pb < b.size()) res.push_back(res.back() + s2[pb++]);
    return convexhull(res);
}
#ifdef USE_DOUBLE
#undef cosl
#undef sinl
#undef sqrtl
#endif

};  // namespace Geometry
using namespace Geometry;