struct point{
double x,y;
point(){}
point(double x,double y):x(x),y(y){}
point operator + (point &p){
return point(x+p.x,y+p.y);
}
point operator - (point &p){
return point(x-p.x,y-p.y);
}
point operator * (double k){
return point(x*k,y*k);
}
point operator / (double k){
return point(x/k,y/k);
}
};
typedef vector<point>polygon;
int cmp(point a,point b)
{
if(a.x!=b.x){
return a.x<b.x;
}else{
return a.y<b.y;
}
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
#define eps (1e-10)
#define CLOCKWISE 1
#define COUNTER_CLOCKWISE -1
int ccw(point a,point b,point c)
{
point base=b-a;
point linshi=c-a;
if(cross(base,linshi)<eps){
return CLOCKWISE;
}else{
return COUNTER_CLOCKWISE;
}
}
polygon andrewscanf(polygon s)
{
int n=s.size();
if(n<3){
return s;
}
sort(s.begin(),s.end(),cmp);
polygon u,l;
u.push_back(s[0]);
u.push_back(s[1]);
l.push_back(s[n-1]);
l.push_back(s[n-2]);
for(int i=2;i<n;i++){
for(int j=u.size();j>=2&&(ccw(u[j-2],u[j-1],s[i])!=CLOCKWISE);j--){
u.pop_back();
}
u.push_back(s[i]);
}
for(int i=n-3;i>=0;i--){
for(int j=l.size();j>=2&&(ccw(l[j-2],l[j-1],s[i])!=CLOCKWISE);j--){
// cout<<s[i].x<<" "<<s[i].y<<" "<<l[j-2].x<<" "<<l[j-2].y<<" "<<l[j-1].x<<" "<<l[j-1].y<<endl;
l.pop_back();
}
l.push_back(s[i]);
}
reverse(l.begin(),l.end());
for(int i=u.size()-2;i>=1;i--){
l.push_back(u[i]);
}
return l;
}
点的凸包
最新推荐文章于 2024-05-18 14:18:55 发布