二维计算几何通用模板

#include <algorithm>

using namespace std;
const double EPS = 1e-10;
const double PI = acos(-1);

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); }

int dcmp(double x) {
    if (fabs(x) < EPS)return 0;
    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 Aera2(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));
}

//Make sure Length(A) != 0 first.
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 DintanceToSegment(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 PolygonAera(Point *p, int n) {
    double aera = 0;
    for (int i = 1; i < n - 1; i++)
        aera += Cross(p[i] - p[0], p[i + 1] - p[0]);
    return aera / 2;
}

struct Circle {
    Point c;
    double r;

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

struct Line {
    Point p;
    Vector v;

    Point point(double t) {
        return Point(p.x + v.x * t, p.y + v.y * t);
    }
};

int GetLineCircleIntersection(Line L, Circle 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);
    t2 = (-f + sqrt(delta)) / (2 * e);
    sol.push_back(L.point(t1));
    sol.push_back(L.point(t2));
    return 2;
}

double Angle(Vector v) { return atan2(v.y, v.x); }

int GetCircleCircleIntersection(Circle C1, Circle 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, Circle C, vector<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.push_back(Rotate(u, PI / 2));
        return 1;
    } else {
        double ang = asin(C.r / dist);
        v.push_back(Rotate(u, -ang));
        v.push_back(Rotate(u, +ang));
        return 2;
    }
}

int isPointInPolygon(Point p, vector<Point> &poly) {
    int cnt = 0;
    size_t n = poly.size();
    for (size_t i = 0; i < n; i++) {
        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)cnt++;
        if (k < 0 && d2 <= 0 && d1 > 0)cnt--;
    }
    if (cnt != 0)return 1;//点在内部
    return 0;//点在外部
}

size_t ConvexHull(vector<Point> &p, vector<Point> &ch) {
    sort(p.begin(), p.end());
    size_t n = p.size(), m = 0;
    for (size_t i = 0; i < n; i++) {
        while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0){
            ch.pop_back();
            m--;
        }
        ch.push_back(p[i]);
        m++;
    }
    size_t k = m;
    for (size_t i = n - 2; i >= 0; i--) {
        while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0){
            ch.pop_back();
            m--;
        }
        ch.push_back(p[i]);
        m++;
    }
    if (n > 1){
        ch.pop_back();
        m--;
    }
    return m;
}

double Rotating_Calipers(vector<Point> &p) {
    double ans = 0;
    vector<Point> ch;
    size_t m = ConvexHull(p, ch);
    ch.push_back(ch[0]);
    for (size_t i = 0, j = 1; i < m; i++) {
        while (Cross(ch[j] - ch[i], ch[i + 1] - ch[i])
               < Cross(ch[j + 1] - ch[i], ch[i + 1] - ch[i]))
            j = (j + 1) % m;
        ans = max(ans, max(Length(ch[i] - ch[j]), Length(ch[i + 1] - ch[j + 1])));
    }
    return ans;
}

int main() {

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值