zoukankan      html  css  js  c++  java
  • 求凸包—— graham_scan算法

    求凸包—— graham_scan算法

    先按Y-X排序,在按对p0的极角排序,然后进行扫瞄

    bool cmp(Point A,Point B)
    {
        //return (A-vert[0])*(B-vert[0])>EPS;
        double tmp=(A-vert[0])*(B-vert[0]);
        if(tmp>EPS) return true;
        if(fabs(tmp)<EPS) return dist(A,vert[0])>dist(B,vert[0])+EPS;///极角相同时,距离远的在前
        return false;
    }
    
    bool cmpYX(Point A,Point B)
    {
        if(A.y<B.y-EPS) return true;
        if(A.y==B.y) return A.x<B.x-EPS;
        return false;
    }
    
    void graham_scan()
    {
        sort(vert,vert+N,cmpYX);
        sort(vert+1,vert+N,cmp);
        top=0;
        con[top++]=vert[0];
        con[top++]=vert[1];
        for(int i=2;i<=N;i++){
            if((con[top-1]-con[top-2])*(vert[i%N]-con[top-1])<-EPS){
                top--;
                con[top++]=vert[i%N];
            }
            else if(fabs((con[top-1]-con[top-2])*(vert[i%N]-con[top-1]))<EPS){///拐向相同时,取距离远的,舍弃距离近的
                if(dist(vert[i%N],con[top-2])>dist(con[top-1],con[top-2])+EPS){
                    top--;
                    con[top++]=vert[i%N];
                }
            }
            else con[top++]=vert[i%N];
            while(top>=3&&(con[top-2]-con[top-3])*(con[top-1]-con[top-2])<EPS){///这步处理是必要的
                Point tmp=con[top-1];
                top-=2;
                con[top++]=tmp;
            }
        }
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    2019 ECfianl
    Codeforces Round #610 (Div. 2)
    IOS设计模式之三:MVC模式
    MVC3快速搭建Web应用(二)
    IOS设计模式之四:观察者模式
    MVC3快速搭建Web应用(一)
    Three20 NetWork
    IOS设计模式之一:单例模式
    IOS设计模式之二:Delegate模式
    写博客的意义
  • 原文地址:https://www.cnblogs.com/--560/p/4397012.html
Copyright © 2011-2022 走看看