求凸包(两遍扫描,求上下凸包的方法)

求凸包模版

struct point
{
    double x,y;
    double val,len;
}points[20];
point points1[20];
point points2[20];

const int INF=1e8;

bool cmp(point a,point b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}

double chaji(point a,point b,point c,point d)
{
    return (b.x-a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x);
}

int real[20];

int cover(int potnum)
{
    sort(points1,points1+potnum,cmp);
    int ansnum=0;
    for(int i=0;i<potnum;i++)
    {
        while(ansnum>1&&chaji(points2[ansnum-2],points2[ansnum-1],points2[ansnum-1],points1[i])<0)
            ansnum--;
        points2[ansnum++]=points1[i];
    }
    int k=ansnum;
    for(int i=potnum-2;i>=0;i--)
    {
        while(ansnum>k&&chaji(points2[ansnum-2],points2[ansnum-1],points2[ansnum-1],points1[i])<0)
            ansnum--;
        points2[ansnum++]=points1[i];
    }
    if(potnum>1)
        ansnum--;
    return ansnum;
}

double way(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

 

posted @ 2016-07-25 13:07  相儒以沫  阅读(730)  评论(0编辑  收藏  举报