凸包

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
 int x,y;
} a[5005],b[5005];
int n,top,row,line;
int cross(const node a1,const node a2,const node b1,const node b2)//(a2-a1)x(b2-b1)
{
 return (a2.x-a1.x)*(b2.y-b1.y)-(a2.y-a1.y)*(b2.x-b1.x);
}
double Dis(const node a1,const node a2)
{
 return sqrt((a2.x-a1.x)*(a2.x-a1.x)+(a2.y-a1.y)*(a2.y-a1.y)*0.1);
}
void get_basic()
{
 int i,j=0;
 node pp;
 pp=a[0];
 for(i=1;i<n;i++)
 {
  if(pp.y>a[i].y||pp.y==a[i].y&&pp.x<a[i].x)
  {
   j=i;
   pp=a[i];
  }
 }
 if(j!=0)
 {
  a[j]=a[0];
  a[0]=pp;
 }
 a[n]=pp;
}
bool cmp(node p1,node p2)
{
 int C=cross(a[0],p1,a[0],p2);
 return C? C>0:Dis(a[0],p1)<Dis(a[0],p2);
}
void tubao()
{
 get_basic();
 sort(a+1,a+n,cmp);
 b[0]=a[0];
 b[1]=a[1];
 int i;
 top=1;
 for(i=2;i<n;i++)
 {
  while(top&&cross(a[top-1],a[top],a[top],a[i])<=0)
  {
   top--;
  }
  b[++top]=a[i];
 }
}

原文地址:https://www.cnblogs.com/beige1315402725/p/4869770.html