zoukankan      html  css  js  c++  java
  • lrj计算几何模板

    整理了一下大白书上的计算几何模板。

      1 #include <cstdio>
      2 #include <algorithm>
      3 #include <cmath>
      4 #include <vector>
      5 using namespace std;
      6 //lrj计算几何模板
      7 struct Point
      8 {
      9     double x, y;
     10     Point(double x=0, double y=0) :x(x),y(y) {}
     11 };
     12 typedef Point Vector;
     13 
     14 Point read_point(void)
     15 {
     16     double x, y;
     17     scanf("%lf%lf", &x, &y);
     18     return Point(x, y);
     19 }
     20 
     21 const double EPS = 1e-10;
     22 
     23 //向量+向量=向量 点+向量=点
     24 Vector operator + (Vector A, Vector B)    { return Vector(A.x + B.x, A.y + B.y); }
     25 
     26 //向量-向量=向量 点-点=向量
     27 Vector operator - (Vector A, Vector B)    { return Vector(A.x - B.x, A.y - B.y); }
     28 
     29 //向量*数=向量
     30 Vector operator * (Vector A, double p)    { return Vector(A.x*p, A.y*p); }
     31 
     32 //向量/数=向量
     33 Vector operator / (Vector A, double p)    { return Vector(A.x/p, A.y/p); }
     34 
     35 bool operator < (const Point& a, const Point& b)
     36 { return a.x < b.x || (a.x == b.x && a.y < b.y); }
     37 
     38 int dcmp(double x)
     39 { if(fabs(x) < EPS) return 0; else return x < 0 ? -1 : 1; }
     40 
     41 bool operator == (const Point& a, const Point& b)
     42 { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
     43 
     44 /**********************基本运算**********************/
     45 
     46 //点积
     47 double Dot(Vector A, Vector B)
     48 { return A.x*B.x + A.y*B.y; }
     49 //向量的模
     50 double Length(Vector A)    { return sqrt(Dot(A, A)); }
     51 
     52 //向量的夹角,返回值为弧度
     53 double Angle(Vector A, Vector B)
     54 { return acos(Dot(A, B) / Length(A) / Length(B)); }
     55 
     56 //叉积
     57 double Cross(Vector A, Vector B)
     58 { return A.x*B.y - A.y*B.x; }
     59 
     60 //向量AB叉乘AC的有向面积
     61 double Area2(Point A, Point B, Point C)
     62 { return Cross(B-A, C-A); }
     63 
     64 //向量A旋转rad弧度
     65 Vector VRotate(Vector A, double rad)
     66 {
     67     return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
     68 }
     69 
     70 //将B点绕A点旋转rad弧度
     71 Point PRotate(Point A, Point B, double rad)
     72 {
     73     return A + VRotate(B-A, rad);
     74 }
     75 
     76 //求向量A向左旋转90°的单位法向量,调用前确保A不是零向量
     77 Vector Normal(Vector A)
     78 {
     79     double l = Length(A);
     80     return Vector(-A.y/l, A.x/l);
     81 }
     82 
     83 /**********************点和直线**********************/
     84 
     85 //求直线P + tv 和 Q + tw的交点,调用前要确保两条直线有唯一交点
     86 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)
     87 {
     88     Vector u = P - Q;
     89     double t = Cross(w, u) / Cross(v, w);
     90     return P + v*t;
     91 }//在精度要求极高的情况下,可以自定义分数类
     92 
     93 //P点到直线AB的距离
     94 double DistanceToLine(Point P, Point A, Point B)
     95 {
     96     Vector v1 = B - A, v2 = P - A;
     97     return fabs(Cross(v1, v2)) / Length(v1);    //不加绝对值是有向距离
     98 }
     99 
    100 //点到线段的距离
    101 double DistanceToSegment(Point P, Point A, Point B)
    102 {
    103     if(A == B)    return Length(P - A);
    104     Vector v1 = B - A, v2 = P - A, v3 = P - B;
    105     if(dcmp(Dot(v1, v2)) < 0)    return Length(v2);
    106     else if(dcmp(Dot(v1, v3)) > 0)    return Length(v3);
    107     else return fabs(Cross(v1, v2)) / Length(v1);
    108 }
    109 
    110 //点在直线上的射影
    111 Point GetLineProjection(Point P, Point A, Point B)
    112 {
    113     Vector v = B - A;
    114     return A + v * (Dot(v, P - A) / Dot(v, v));
    115 }
    116 
    117 //线段“规范”相交判定
    118 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
    119 {
    120     double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
    121     double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
    122     return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
    123 }
    124 
    125 //判断点是否在线段上
    126 bool OnSegment(Point P, Point a1, Point a2)
    127 {
    128     Vector v1 = a1 - P, v2 = a2 - P;
    129     return dcmp(Cross(v1, v2)) == 0 && dcmp(Dot(v1, v2)) < 0;
    130 }
    131 
    132 //求多边形面积
    133 double PolygonArea(Point* P, int n)
    134 {
    135     double ans = 0.0;
    136     for(int i = 1; i < n - 1; ++i)
    137         ans += Cross(P[i]-P[0], P[i+1]-P[0]);
    138     return ans/2;
    139 }
    140 
    141 int main(void)
    142 {
    143     Vector a[2];
    144     sort(a, a + 2);
    145     return 0;
    146 }
    147 
    148 /**********************圆的相关计算**********************/
    149 
    150 const double PI = acos(-1.0);
    151 struct Line
    152 {//有向直线
    153     Point p;
    154     Vector v;
    155     double ang;
    156     Line()    { }
    157     Line(Point p, Vector v): p(p), v(v)    { ang = atan2(v.y, v.x); }
    158     Point point(double t)
    159     {
    160         return p + v*t;
    161     }
    162     bool operator < (const Line& L) const
    163     {
    164         return ang < L.ang;
    165     }
    166 };
    167 
    168 struct Circle
    169 {
    170     Point c;    //圆心
    171     double r;    //半径
    172     Circle(Point c, double r):c(c), r(r)    {}
    173     Point point(double a)
    174     {//求对应圆心角的点
    175         return Point(c.x + r*cos(a), c.y + r*sin(a));
    176     }
    177 };
    178 
    179 //两圆相交并返回交点个数 
    180 int getLineCircleIntersection(Line L, Circle C, double& t1, double& t2, vector<Point>& sol)
    181 {
    182     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
    183     double e = a*a + c*c, f = 2*(a*b + c*d), g = b*b + d*d - C.r*C.r;
    184     double delta = f*f - 4*e*g;        //判别式
    185     if(dcmp(delta) < 0)    return 0;    //相离
    186     if(dcmp(delta) == 0)            //相切
    187     {
    188         t1 = t2 = -f / (2 * e);
    189         sol.push_back(L.point(t1));
    190         return 1;
    191     }
    192     //相交
    193     t1 = (-f - sqrt(delta)) / (2 * e);    sol.push_back(L.point(t1));
    194     t2 = (-f + sqrt(delta)) / (2 * e);    sol.push_back(L.point(t2));
    195     return 2;
    196 }
    197 
    198 //计算向量极角
    199 double angle(Vector v)    { return atan2(v.y, v.x); }
    200 
    201 int getCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol)
    202 {//圆与圆相交,并返回交点个数
    203     double d = Length(C1.c - C2.c);
    204     if(dcmp(d) == 0)
    205     {
    206         if(dcmp(C1.r - C2.r) == 0)    return -1;    //两圆重合
    207         return 0;                                //没有交点
    208     }
    209     if(dcmp(C1.r + C2.r - d) > 0)    return 0;
    210     if(dcmp(fabs(C1.r - C2.r) - d) > 0)    return 0;
    211 
    212     double a = angle(C2.c - C1.c);
    213     double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d));
    214     Point p1 = C1.point(a+da), p2 = C1.point(a-da);
    215     sol.push_back(p1);
    216     if(p1 == p2)    return 1;
    217     sol.push_back(p2);
    218     return 2;
    219 }
    220 
    221 //过定点作圆的切线并返回切线条数
    222 int getTangents(Point p, Circle C, Vector* v)
    223 {
    224     Vector u = C.c - p;
    225     double dist = Length(u);
    226     if(dist < C.r)    return 0;
    227     else if(dcmp(dist - C.r) == 0)
    228     {
    229         v[0] = VRotate(u, PI/2);
    230         return 1;
    231     }
    232     else
    233     {
    234         double ang = asin(C.r / dist);
    235         v[0] = VRotate(u, +ang);
    236         v[1] = VRotate(u, -ang);
    237         return 2;
    238     }
    239 }
    240 
    241 //求两个圆的公切线,并返回切线条数
    242 //注意,这里的Circle和上面的定义的Circle不一样
    243 int getTangents(Circle A, Circle B, Point* a, Point* b)
    244 {
    245     int cnt = 0;
    246     if(A.r < B.r)    { swap(A, B); swap(a, b); }
    247     double d2 = (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
    248     double rdiff = A.r - B.r;
    249     double rsum = A.r + B.r;
    250     if(d2 < rdiff*rdiff)    return 0;    //内含
    251 
    252     double base = atan2(B.y-A.y, B.x-A.x);
    253     if(dcmp(d2) == 0 && dcmp(A.r - B.r) == 0)    return -1; //重合
    254     if(dcmp(d2 - rdiff*rdiff) == 0)    //内切
    255     {
    256         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
    257         return 1;
    258     }
    259 
    260     //有外公切线
    261     double ang = acos((A.r - B.r) / sqrt(d2));
    262     a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
    263     a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
    264     if(dcmp(rsum*rsum - d2) == 0)
    265     {//外切
    266         a[cnt] = b[cnt] = A.point(base); cnt++;
    267     }
    268     else if(dcmp(d2 - rsum*rsum) > 0)
    269     {
    270         ang = acos((A.r + B.r) / sqrt(d2));
    271         a[cnt] = A.point(base + ang); b[cnt] = B.point(PI + base + ang); cnt++;
    272         a[cnt] = A.point(base - ang); b[cnt] = B.point(PI + base - ang); cnt++;
    273     }
    274     return cnt;
    275 }
    276 
    277 //转角发判定点P是否在多边形内部
    278 int isPointInPolygon(Point P, Point* Poly, int n)
    279 {
    280     int wn;
    281     for(int i = 0; i < n; ++i)
    282     {
    283         if(OnSegment(P, Poly[i], Poly[(i+1)%n]))    return -1;    //在边界上
    284         int k = dcmp(Cross(Poly[(i+1)%n] - Poly[i], P - Poly[i]));
    285         int d1 = dcmp(Poly[i].y - P.y);
    286         int d2 = dcmp(Poly[(i+1)%n].y - P.y);
    287         if(k > 0 && d1 <= 0 && d2 > 0)    wn++;
    288         if(k < 0 && d2 <= 0 && d1 > 0)    wn--;
    289     }
    290     if(wn != 0)    return 1;    //内部
    291     return 0;                //外部
    292 }
    293 
    294 //计算凸包,输入点数组P,个数为n,输出点数组ch。函数返回凸包顶点数。
    295 //输入不能有重复点,函数执行后点的顺序会发生变化
    296 //如果不希望凸包的边上有输入点,把两个 <= 改成 <
    297 //在精度要求高时,可用dcmp比较
    298 int ConvexHull(Point* p, int n, Point* ch)
    299 {
    300     sort(p, p +n);
    301     int m = 0;
    302     for(int i = 0; i < n; ++i)
    303     {
    304         while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)    m--;
    305         ch[m++] = p[i];
    306     }
    307     int k = m;
    308     for(int i = n-2; i >= 0; --i)
    309     {
    310         while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0)    m--;
    311         ch[m++] = p[i];
    312     }
    313     if(n > 1)    m--;
    314     return m;
    315 }

    旋转卡壳的模板:

    int diameter2(vector<Point>& points)
    {
        vector<Point> p = ConvexHull(points);
        int n = p.size();
        //for(int i = 0; i < n; ++i)    printf("%d %d
    ", p[i].x, p[i].y);
        if(n == 1)    return 0;
        if(n == 2)  return Dist2(p[0], p[1]);
        p.push_back(p[0]);
        int ans = 0;
        for(int u = 0, v = 1; u < n; ++u)
        {// 一条直线贴住边p[u]-p[u+1]
            while(true)
            {
                // 当Area(p[u], p[u+1], p[v+1]) <= Area(p[u], p[u+1], p[v])时停止旋转
                //因为两个三角形有一公共边,所以面积大的那个点到直线距离大 
                // 即Cross(p[u+1]-p[u], p[v+1]-p[u]) - Cross(p[u+1]-p[u], p[v]-p[u]) <= 0
                // 根据Cross(A,B) - Cross(A,C) = Cross(A,B-C)
                // 化简得Cross(p[u+1]-p[u], p[v+1]-p[v]) <= 0
                int diff = Cross(p[u+1]-p[u], p[v+1]-p[v]);
                if(diff <= 0)
                {
                    ans = max(ans, Dist2(p[u], p[v]));
                    if(diff == 0)    ans = max(ans, Dist2(p[u], p[v+1]));
                    break;
                } 
                v = (v+1)%n;
            }
        }
        return ans;
    }
  • 相关阅读:
    设计模式 里氏替换原则
    java队列
    java 多线程
    设计模式-里氏替换原则
    设计模式-单一职责原则
    一、概念
    六、序列化和反序列化(对象流)
    七、随机访问文件流
    五、包装流
    四、字符输入输出流
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4022401.html
Copyright © 2011-2022 走看看