计算几何-Andrew法-凸包

This article is made by Jason-Cow.
Welcome to reprint.
But please post the article's address.

 

利用一下叉积

 1 int Andrew(D*R,int n,D*A){
 2     int m=0;
 3     sort(R+1,R+n+1);//for(int i=1;i<=n;i++)cout<<R[i].x<<" "<<R[i].y<<endl;
 4     for(int i=1;i<=n;i++){
 5         while(m>=2 && Cross(A[m]-A[m-1],R[i]-A[m-1])<=0)m--;
 6         A[++m]=R[i];
 7     }
 8     int k=m;
 9     for(int i=n-1;i>=1;i--){
10         while(m>k  && Cross(A[m]-A[m-1],R[i]-A[m-1])<=0)m--;
11         A[++m]=R[i];
12     }
13     return n>1?m-1:m;
14 }

 

posted @ 2017-03-20 14:00  墨鳌  阅读(236)  评论(0编辑  收藏  举报