• 刘汝佳计算几何模板--线段与向量部分


    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int N = 1e6+10;
    const double eps = 1e-10;//精度
    //atan2(y,x)求极角的弧度
    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)//判断位置,先按照x坐标排序然后按照y坐标排序
    {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
    
    int dcmp(double x)//判断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) == 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));
    }
    //A×B,B在A的左边是正,在右边是负
    double Cross(Vector A,Vector B)//两个向量的叉积
    {
        return A.x*B.y - A.y*B.x;
    }
    double Area2(Point A,Point B,Point C)//ABC三角形有向面积的两倍
    {
        return Cross(B-A,C-A);
    }
    Vector Rotate(Vector A,double rad)//向量逆时针旋转rad
    {
        return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
    }
    //把向量向左旋转90°,得到法向量,得保证A向量不是零向量
    Vector Normal(Vector A)
    {
        double L = Length(A);
        return Vector(-A.y/L,A.x/L);
    }
    //两条直线的交点,向量式--参数方程.前提是v,w向量不平行,Cross(v,w)!=0
    Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
    {
        Vector u = P - Q;
        double t = Cross(w,u) / Cross(v,w);//P,v直线的参数t
        return P + v*t;
    }
    //P到直线A-B距离
    double DistanceToLine(Point P, Point A, Point B)
    {
        Vector v1=B-A, v2=P-A;
        return fabs(Cross(v1,v2) / Length(v1));
    }
    //P到线段A-B距离
    //设投影点为Q,如果Q在线段AB上,则所求距离就是P点直线AB的距离,如果Q在射线BA上,则所求距离为PA距离;否则为PB距离。
    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);//PA距离
        else if(dcmp(Dot(v1,v3)) > 0) return Length(v3);//PB距离
        else return fabs(Cross(v1,v2)) / Length(v1);//PQ距离
    }
    //求P到直线A-B垂点
    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;
    }
    //p点是否在a1,a2线段上(不在a1,a2点上)
    bool OnSegment(Point p, Point a1, Point a2)
    {
        return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p)) < 0;
    }
    //判断多边形的面积(凸,非凸都行)
    double PolygomArea(Point *p, int n)
    {
        double area = 0;
        for(int i=1;i<n;i++)
        {
            area += Cross(p[i]-p[0],p[i+1]-p[0]);
        }
        return area/2;
    }
    //Andrew算法求凸包
    //return值是凸包的边数,ch存凸包的点
    int ConvexHull(Point *p,int n,Point *ch)
    {
        sort(p,p+n);
        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(m > 1)
            m--;
        return m;
    }
    void solve()
    {
        
    }
    int main()
    {
        int T;
        cin >> T;
        while(T--)
        {
            solve();
        }
        return 0;
    }
    
    
    
  • 相关阅读:
    232.8.flask特殊装饰器
    233.9.flask闪现
    238.14.flask简单项目
    237.13.flask上下文管理
    234.10.flask中间件
    231.7.flasksession
    236.12.flaskthreading.local
    多测师拱墅校区肖sir_高级金牌讲师_selenium实战(3)
    多测师拱墅校区肖sir_高级金牌讲师_rf自动化框架常用的关键字(3)
    多测师拱墅校区肖sir_高级金牌讲师_python 之总结和面试题
  • 原文地址:https://www.cnblogs.com/hh13579/p/13585315.html
Copyright © 2020-2023  润新知