• 计算几何模板


    先开帖..以后慢慢完善.....

    首先是struct point.自定义"点"类.用来存向量.

    struct point
    {
        db x; db y;
        point(db a=0,db b=0):x(a),y(b){}
        //赋值
        point operator=(point f)
        { x=f.x; y=f.y; return *this; }
        
        //向量和
        point operator+(point f)
        { return point(x+f.x,y+f.y); }
        
        //向量差
        point operator-(point f)
        { return point(x-f.x,y-f.y); }
        
        //从本顶点出发,指向另一个点的向量
        point operator()(point f)
        { return f-(*this); }
        
        //数乘
        point operator*(int f) 
        { return point(x*f,y*f); }
        
        //叉积
        db operator*(point f)
        { return x*f.y - y*f.x; }
        
        //点积
        db operator/(point f)
        { return x*f.x + y*f.y; }
        
        //向量的模
        db length()
        { return sqrt(x*x+y*y); }
        
        //判断是否相等
        bool operator==(point f)
        { return x==f.x && y==f.y; }
        
        //判断本向量在另一个向量的逆时针方向
        bool operator<<(point f) 
        { return (*this)*f<0; }
        
        //判断本向量在另一个向量的逆时针方向或同向 
        bool operator<<=(point f) 
        { return (*this)*f<=0; }
        
        //判断本向量在另一个向量的顺时针方向
        bool operator>>(point f) 
        { return (*this)*f>0; }
        
        
        //判断本向量在另一个向量的顺时针方向或同向 
        bool operator>>=(point f) 
        { return (*this)*f>=0; }
        
        //按照y为第一关键字,x为第二关键字进行比较
        bool operator<(const point f) const
        { return y<f.y || (y==f.y && x<f.x); }
    };

     
    线段

    struct segment
    {
        point b,d;
        segment(){ d=b=point(); }
        segment(db x1,db y1,db x2,db y2):b(point(x1,y1)),d(point(x2,y2)){}
        segment(point a,point b):b(a),d(b){}
        
        bool operator/(const segment&f) //parallel
        { db k=f.d.x/d.x; if(feq(k*d.y,f.d.y)) return true; return false; }
        
        bool operator*(segment f) //intersect
        {
            return  (d*( (f.b+f.d)-(b+d) ))*(d*( (f.b)-(b+d) ))<=0.0 &&
                    (f.d*( (b+d)-(f.b+f.d) ))*(f.d*( (b)-(f.b+f.d) ))<=0.0 ;
        }
        
    };

    点在线端上: 点到线段两端点的长度和等于线段长.

    线段相交(内交): 每个线段的两个端点分别在另一条线段两侧.

    线段相交: 线段内交,或一个线段的一个端点在另一个线段上.

     


    接着是凸包.....

    发现凸包竟敢如此好写.....

     

    void GRAHAM()
    {
        sort(a,a+n);
        
        st=0;
        
        for(int i=0;i<n;i++)
        {
            while( st>1 &&( stk[st-2](stk[st-1])>>stk[st-1](a[i])) ) st--;
            stk[st++]=a[i];
        }
        int p=st;
        for(int i=n-2;i>=0;i--)
        {
            while( st>p && ( stk[st-2](stk[st-1])>>stk[st-1](a[i])) ) st--;
            stk[st++]=a[i];
        }
        
    }

     

    注意如果只想要凸包的顶点,那么程序中的>>(判断顺时针方向)要改成>>=(判断顺时针方向或同向)

     

    接着是旋转卡壳

     

    int res=0;
    
    int p=2;
    for(int i=1;i<st;i++) //segment:stk[i-1],stk[i] vertix: stk[p].
    {
        while( p!=st && area(stk[i-1], stk[i], stk[p+1]) >
            area(stk[i-1], stk[i], stk[p]))
            p++;
            
        if(p==st) break;
        
        res=max(res,stk[i-1](stk[p]).dist2());
        res=max(res,stk[i](stk[p]).dist2());
    }

     

     

    程序求的是凸包上最远点对.求别的东西需要改写.

    注意p==st后,我们已经枚举完所有对踵点对了,此时可以直接跳出循环.

    不跳的话stk[p]这个没有被赋值的点会被访问.....


    这个模板没有特判一些奇妙的细节......也没有在意精度......特别注意.....


    =w=







     

  • 相关阅读:
    在vs2010里使用EF4.3的Code First个人使用笔记
    如何充分利用C#匿名方法的平台优势(转)
    打开Microsoft SQL Server Management Studio 2005非常慢,特别慢的原因
    PowerShell msbuild
    很久没写了
    第一篇博客,逗大家一笑
    以编程方式调用按钮1(button1)的 Click 事件
    VS2010删除已安装的联机模板
    创建和分享你的Visual Studio color
    异步获取CMD命令行输出内容
  • 原文地址:https://www.cnblogs.com/DragoonKiller/p/4295952.html
Copyright © 2020-2023  润新知