几何模板

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define REV(i, n) for(int i = (n); i >= 0; i--)
#define MEM(str) memset(str, 0, sizeof(str))
#define MEM_1(str) memset(str, -1, sizeof(str))
#define ALL(vec) vec.begin(),vec.end()

typedef long long LL;
typedef const int CI;
typedef unsigned long long ULL;

const double PI = acos(-1);
const int INF = ((1 << 31) - 1);

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
int lcm(int a, int b) { return a / gcd(a, b)*b; }
double torad(double deg) { return deg / 180 * PI; }
struct Point {
    double x, y;
    Point(double x = 0, double y = 0) :x(x), y(y) {}
};
typedef Point Vector;
Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator*(Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator<(const Point& a, const Point& b) { return a.x < b.x | (a.x == b.x && a.y < b.y); }
const double eps = 1e-10;
int dcmp(double x) { if (x < eps)return 0; else return x < 0 ? -1 : 1; }
bool operator==(const Point& a, const Point& b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }
Vector Rotate(Vector A, double rad) { return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad)); }
Vector Normal(Vector A) { double L = Length(A); return Vector(-A.y / L, A.x / L); }
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
    Vector u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v*t;
}
double DistanceToLine(Point P, Point A, Point B) {
    Vector v1 = B - A, v2 = P - A;
    return fabs(Cross(v1, v2) / Length(v1));
}
double DistanceToSegment(Point P, Point A, Point B) {
    if (A == B)return Length(P - A);
    Vector v1 = B - A, v2 = P - A, v3 = P - B;
    if (dcmp(Dot(v1, v2)) < 0)return Length(v2);
    else if (dcmp(Dot(v1, v3)) > 0)return Length(v3);
    else return fabs(Cross(v1, v2)) / Length(v1);
}
Point GetLineProjection(Point P, Point A, Point B) { Vector v = B - A; return A + v*(Dot(v, P - A) / Dot(v, v)); }
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 p, Point a1, Point a2) { return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; }
double PolygonArea(Point* p, int n) {
    double area = 0;
    for (int i = 1; i < n - 1; i++) area += Cross(p[i] - p[0], p[i + 1] - p[0]);
    return area / 2;
}

struct Line {
    Point p;
    Vector v;
    double ang;
    Line() {}
    Line(Point p, Vector v) :p(p), v(v) { ang = atan2(v.y, v.x); }
    Point point(double t) { return Point(p.x + v.x*t, p.y + v.y*t); }
    bool operator<(const Line& L)const { return ang < L.ang; }
};

struct Cirle {
    Point c;
    double r;
    Cirle(Point c, double r) :c(c), r(r) {}
    Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
};

int GetLineCirleIntersection(Line L, Cirle C, double& t1, double& t2, vector<Point>& sol) {
    double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
    double e = a*a + c*c, f = 2 * (a*b + c*d), g = b*b + d*d - C.r*C.r;
    double delta = f*f - 4 * e*g;
    if (dcmp(delta) < 0)return 0;
    if (dcmp(delta) == 0) {
        t1 = t2 = -f / (2 * e); sol.push_back(L.point(t1));
        return 1;
    }
    t1 = (-f - sqrt(delta) / (2 * e)); sol.push_back(L.point(t1));
    t2 = (-f + sqrt(delta) / (2 * e)); sol.push_back(L.point(t2));
    return 2;
}
double angle(Vector v) { return atan2(v.y, v.x); }
int GetCirleIntersection(Cirle C1, Cirle C2, vector<Point>& sol) {
    double d = Length(C1.c - C2.c);
    if (dcmp(d) == 0) {
        if (dcmp(C1.r - C2.r) == 0)return -1;
        return 0;
    }
    if (dcmp(C1.r + C2.r - d) < 0)return 0;
    if (dcmp(fabs(C1.r - C2.r) - d) > 0)return 0;
    double a = angle(C2.c - C1.c);
    double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2 * C1.r*d));
    Point p1 = C1.point(a - da), p2 = C1.point(a + da);
    sol.push_back(p1);
    if (p1 == p2) return 1;
    sol.push_back(p2);
    return 2;
}

int GetTangents(Point p, Cirle C, Vector* v) {
    Vector u = C.c - p;
    double dist = Length(u);
    if (dist < C.r)return 0;
    else if (dcmp(dist - C.r) == 0) { v[0] = Rotate(u, PI / 2); return 1; }
    else {
        double ang = asin(C.r / dist);
        v[0] = Rotate(u, -ang);
        v[1] = Rotate(u, +ang);
        return 2;
    }
}

int GetTangents(Cirle A, Cirle B, Point* a, Point* b) {
    int cnt = 0;
    if (A.r < B.r) { swap(A, B); swap(a, b); }
    int d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);
    int rdiff = A.r - B.r;
    int rsum = A.r + B.r;
    if (d2 < rdiff*rdiff) return 0;
    double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
    if (d2 == 0 && A.r == B.r)return -1;
    if (d2 == rdiff*rdiff) {
        a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
        return 1;
    }
    double ang = acos((A.r - B.r) / sqrt(d2));
    a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
    a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
    if (d2 == rsum*rsum) {
        a[cnt] = A.point(base); b[cnt] = B.point(PI + base); cnt++;
    }
    else if (d2 > rsum*rsum) {
        double ang = acos((A.r + B.r) / sqrt(d2));
        a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
        a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
    }
    return cnt;
}
typedef vector<Point> Polygon;
void toUnit(Vector& v) { double len = Length(v); v = v / len; }

int IsPointInPolygon(Point p, Polygon poly) {
    int wn = 0;
    int n = poly.size();
    REP(i, n) {
        if (OnSegment(p, poly[i], poly[(i + 1) % n]))return -1;
        int k = dcmp(Cross(poly[(i + 1) % n] - poly[i], p - poly[i]));
        int d1 = dcmp(poly[i].y - p.y);
        int d2 = dcmp(poly[(i + 1) % n].y - p.y);
        if (k > 0 && d1 <= 0 && d2 > 0)wn++;
        if (k < 0 && d2 <= 0 && d1 > 0)wn--;
    }
    if (wn != 0)return 1;
    return 0;
}

int ConvexHull(Point* p, int n, Point* ch) {
    sort(p, p + n);
    int m = 0;
    REP(i, n) {
        while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)m--;
        ch[m++] = p[i];
    }
    int k = m;
    REV(i, n - 2) {
        while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)m--;
        ch[m++] = p[i];
    }
    if (n > 1)m--;
    return m;
}

bool OnLeft(Line L, Point p) { return Cross(L.v, p - L.p) > 0; }
Point GetIntersection(Line a, Line b) { Vector u = a.p - b.p; double t = Cross(b.v, u) / Cross(a.v, b.v); return a.p + a.v*t; }
int HalfplaneIntersection(Line* L, int n, Point* poly) {
    sort(L, L + n);
    int first, last;
    Point* p = new Point[n];
    Line *q = new Line[n];
    q[first = last = 0] = L[0];
    FOR(i, 1, n - 1) {
        while (first < last && !OnLeft(L[i], p[last - 1]))last--;
        while (first < last && !OnLeft(L[i], p[first]))first++;
        q[++last] = L[i];
        if (fabs(Cross(q[last].v, q[last - 1].v)) < eps) {
            last--;
            if (OnLeft(q[last], L[i].p))q[last] = L[i];
        }
        if (first < last)p[last - 1] = GetIntersection(q[last - 1], q[last]);
    }
    while (first < last && !OnLeft(q[first], p[last - 1]))last--;
    if (last - first <= 1)return 0;
    p[last] = GetIntersection(q[last], q[first]);
    int m = 0;
    FOR(i, first, last)poly[m++] = p[i];
    return m;
}
struct Point3 {
    double x, y, z;
    Point3(double x = 0, double y = 0, double z = 0) :x(x), y(y), z(z) { }
};
typedef Point3 Vector3;
Vector3 operator+(Vector3 A, Vector3 B) { return Vector3(A.x + B.x, A.y + B.y, A.z + B.z); }
Vector3 operator-(Vector3 A, Vector3 B) { return Vector3(A.x - B.x, A.y - B.y, A.z - B.z); }
Vector3 operator*(Vector3 A, double p) { return Vector3(A.x*p, A.y*p, A.z*p); }
Vector3 operator/(Vector3 A, double p) { return Vector3(A.x / p, A.y / p, A.z / p); }
bool operator==(const Vector3& A,const Vector3& B) { return A.x == B.x && A.y == B.y && A.z == B.z; }
bool operator<(const Vector3& A, const Vector3& B) { return A.x < B.x | (A.x == B.x && A.y < B.y) | (A.x == B.x && A.y == B.y && A.z < B.z); }
double Dot(Vector3 A, Vector3 B) { return A.x*B.x + A.y*B.y + A.z*B.z; }
double Length(Vector3 A) { return sqrt(Dot(A, A)); }
double Angle(Vector3 A, Vector3 B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double DistanceToPlane(const Point3& p, const Point3& p0, const Vector3& n) { return fabs(Dot(p - p0, n)); }
Point3 GetPlaneProjection(const Point3& p, const Point3& p0, const Vector3& n) { return p - n*Dot(p - p0, n); }
Point3 LinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n) {
    Vector3 v = p2 - p1;
    double t = (Dot(n, p0 - p1) / Dot(n, p2 - p1));
    return p1 + v*t;
}
Vector3 Cross(Vector3 A, Vector3 B) { return Vector3(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.x*B.y - A.y*B.x); }
double Area2(Point3 A, Point3 B, Point3 C) { return Length(Cross(B - A, C - A)); }
bool PointInTri(Point3 P, Point3 P0, Point3 P1, Point3 P2) {
    double area1 = Area2(P, P0, P1);
    double area2 = Area2(P, P1, P2);
    double area3 = Area2(P, P2, P0);
    return dcmp(area1 + area2 + area3 - Area2(P0, P1, P2)) == 0;
}
bool TriSegIntersection(Point3 P0, Point3 P1, Point3 P2, Point3 A, Point3 B, Point3 P) {
    Vector3 n = Cross(P1 - P0, P2 - P0);
    if (dcmp(Dot(n, B - A)) == 0)return false;
    else {
        double t = Dot(n, P0 - A) / Dot(n, B - A);
        if (dcmp(t) < 0 | dcmp(t - 1) > 0) return false;
        P = A + (B - A)*t;
        return PointInTri(P, P0, P1, P2);
    }
}
double DistanceToLine(Point3 P, Point3 A, Point3 B) {
    Vector3 v1 = B - A, v2 = P - A;
    return Length(Cross(v1, v2)) / Length(v1);
}
double DistanceToSegment(Point3 P, Point3 A, Point3 B) {
    if (A == B)return Length(P - A);
    Vector3 v1 = B - A, v2 = P - A, v3 = P - B;
    if (dcmp(Dot(v1, v2)) < 0)return Length(v2);
    else if (dcmp(Dot(v1, v3)) > 0)return Length(v3);
    else return Length(Cross(v1, v2)) / Length(v1);
}
double Volume6(Point3 A, Point3 B, Point3 C, Point3 D) { return Dot(D - A, Cross(B - A, C - A)); }

struct Face {
    int v[3];
    Face(){}
    Face(int v0 = 0, int v1 = 1, int v2 = 2) { v[0] = v0; v[1] = v1; v[2] = v2; }
    Vector3 normal(Point3 *P)const { return Cross(P[v[1]] - P[v[0]], P[v[2]] - P[v[0]]); }
    int cansee(Point3* P, int i)const {
        return Dot(P[i] - P[v[0]], normal(P)) > 0 ? 1 : 0;
    }
};

vector<Face> CH3D(Point3* P, int n) {
    vector<Face> cur;
    int vis[3][3];
    cur.push_back(Face(0, 1, 2));
    cur.push_back(Face(2, 1, 0));
    for (int i = 3; i < n; i++) {
        vector<Face> next;
        for (int j = 0; j, cur.size(); j++) {
            Face& f = cur[j];
            int res = f.cansee(P, i);
            if (!res)next.push_back(f);
            REP(k, 3)vis[f.v[k]][f.v[(k + 1) % 3]] = res;
        }
        for (int j = 0; j < cur.size(); j++) {
            REP(k, 3) {
                int a = cur[j].v[k], b = cur[j].v[(k + 1) % 3];
                if (vis[a][b] != vis[b][a] && vis[a][b])next.push_back(Face(a, b, i));
            }
        }
        cur = next;
    }
    return cur;
}
double rand01() { return rand() / (double)RAND_MAX; }
double randeps() { return (rand01() - 0.5)*eps; }
Point3 add_noise(Point3 p) { return Point3(p.x + randeps(), p.y + randeps(), p.z + randeps()); }



int main() {

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值