首页 > 代码库 > (凸包模板)(刘汝佳)

(凸包模板)(刘汝佳)

struct point
{
int x,y;
} p[N],stack[N];

bool cmp(point A,point B)
{
if(A.y==B.y)return A.x<B.x;
return A.y<B.y;
}
int cross(point A,point B,point C)
{
return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);
}
void graham()
{
sort(p,p+n,cmp);
int i;
top=0;
for(i=0; i<n; i++)
{
while(top>1&&cross(stack[top-2],stack[top-1],p[i])<0)
{
top--;
}
stack[top++]=p[i];
}
int t=top;
for(i=n-2; i>=0; i--)
{
while(top>t&&cross(stack[top-2],stack[top-1],p[i])<0)
top--;
stack[top++]=p[i];
}
if(n>1)
top--;
}