平面計算幾何模板

該模板基於劉汝佳算法競賽入門經典--訓練指南

平面計算幾何模板

轉載請註明:轉自http://blog.csdn.net/a15129395718

新的獨立博客,歡迎訪問: http://zihengoi.cn

#include <bits/stdc++.h>
using namespace std;

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.x*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.x/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(fabs(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);
}

//向量a的極角
double Angle(const Vector& v) {
    return atan2(v.y, v.x);\share\CodeBlocks\templates\wizard\console\cpp
}

//向量點積
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }

//向量長度\share\CodeBlocks\templates\wizard\console\cpp
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); }

//向量逆時針旋轉rad度(弧度)
Vector Rotate(Vector A, double rad) {
    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}

//計算向量A的單位法向量。左轉90°,把長度歸一。調用前確保A不是零向量。
Vector Normal(Vector A) {
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}

/************************************************************************
使用複數類實現點及向量的簡單操作

#include <complex>
typedef complex<double> Point;
typedef Point Vector;

double Dot(Vector A, Vector B) { return real(conj(A)*B)}
double Cross(Vector A, Vector B) { return imag(conj(A)*B);}
Vector Rotate(Vector A, double rad) { return A*exp(Point(0, rad)); }

*************************************************************************/

/****************************************************************************
* 用直線上的一點p0和方向向量v表示一條指向。直線上的所有點P滿足P = P0+t*v;
* 如果知道直線上的兩個點則方向向量爲B-A, 所以參數方程爲A+(B-A)*t;
* 當t 無限制時, 該參數方程表示直線。
* 當t > 0時, 該參數方程表示射線。
* 當 0 < t < 1時, 該參數方程表示線段。
*****************************************************************************/

//直線交點,須確保兩直線有唯一交點。
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 DistanceToSegmentS(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);
    double 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 ConvexPolygonArea(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;
}

//計算多邊形的有向面積
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;
}

/***********************************************************************
* Morley定理:三角形每個內角的三等分線,相交成的三角形是等邊三角形。
* 歐拉定理:設平面圖的定點數,邊數和麪數分別爲V,E,F。則V+F-E = 2;
************************************************************************/

struct Circle {
    Point c;
    double r;

    Circle(Point c, double r) : c(c), r(r) {}
    //通過圓心角確定圓上座標
    Point point(double a) {
        return Point(c.x + cos(a)*r, c.y + sin(a)*r);
    }
};

struct Line {
    Point p;
    Vector v;
    double ang;
    Line() {}
    Line(Point p, Vector v) : p(p), v(v) {}
    bool operator < (const Line& L) const {
        return ang < L.ang;
    }
};

//直線和圓的交點,返回交點個數,結果存在sol中。
//該代碼沒有清空sol。
int getLineCircleIntersecion(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(C.point(t1));
        return 1;
    }
    //相交
    t1 = (-f - sqrt(delta)) / (2*e); sol.push_back(C.point(t1));
    t2 = (-f + sqrt(delta)) / (2*e); sol.push_back(C.point(t2));
    return 2;
}

//兩圓相交
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) == 0)) return -1;

    double a = Angle(C2.c - C1.c);               //向量C1C2的極角
    double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d));
    //C1C2到C1P1的角
    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;
}

const double PI = acos(-1);
//過定點做圓的切線
//過點p做圓C的切線,返回切線個數。v[i]表示第i條切線
int getTangents(Point p, Circle 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;
    }
}

//兩圓的公切線
//返回切線的個數,-1表示有無數條公切線。
//a[i], b[i] 表示第i條切線在圓A,圓B上的切點
int getTangents(Circle A, Circle 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(PI+base+ang); cnt++;
        a[cnt] = A.point(base-ang); b[cnt] = B.point(PI+base-ang); cnt++;
    }
    return cnt;
}

typedef vector<Point> Polygon;

//點在多邊形內的判定
int isPointInPolygon(Point p, Polygon poly) {
    int wn = 0;
    int n = poly.size();
    for(int 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) wn++;
        if(k < 0 && d2 <= 0 && d1 > 0) wn++;
    }
    if(wn != 0) return 1;       //內部
    return 0;                   //外部
}

//凸包
/***************************************************************
* 輸入點數組p, 個數爲p, 輸出點數組ch。 返回凸包頂點數
* 不希望凸包的邊上有輸入點,把兩個<= 改成 <
* 高精度要求時建議用dcmp比較
* 輸入點不能有重複點。函數執行完以後輸入點的順序被破壞
****************************************************************/
int ConvexHull(Point *p, int n, Point* ch) {
    sort(p, p+n);      //先比較x座標,再比較y座標
    int m = 0;
    for(int i = 0; i < n; i++) {
        while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    for(int i = n-2; i >= 0; i++) {
        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;
}

//用有向直線A->B切割多邊形poly, 返回“左側”。 如果退化,可能會返回一個單點或者線段
//複雜度O(n2);
Polygon CutPolygon(Polygon poly, Point A, Point B) {
    Polygon newpoly;
    int n = poly.size();
    for(int i = 0; i < n; i++) {
        Point C = poly[i];
        Point D = poly[(i+1)%n];
        if(dcmp(Cross(B-A, C-A)) >= 0) newpoly.push_back(C);
        if(dcmp(Cross(B-A, C-D)) != 0) {
            Point ip = GetLineIntersection(A, B-A, C, D-C);
            if(OnSegment(ip, C, D)) newpoly.push_back(ip);
        }
    }
    return newpoly;
}

//半平面交

//點p再有向直線L的左邊。(線上不算)
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];    //p[i]爲q[i]和q[i+1]的交點
    Line *q = new Line[n];      //雙端隊列
    q[first = last = 0] = L[0]; //隊列初始化爲只有一個半平面L[0]
    for(int i = 0; i < n; i++) {
        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]);

    //從deque複製到輸出中
    int m = 0;
    for(int i = first; i <= last; i++) poly[m++] = p[i];
    return m;
}
int main() {

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章