基于水平序的Andrew凸包算法 最详细的图解(多图预警)

给出凸包的定义:

简要说一下思路:

首先将所有点按照x从小到大(x同则y从小到大)排序
把p1,p2放入凸包,从p3开始,当新点在凸包‘前进’方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边
输入不能有重复点,不希望凸包边上有点可疑将<=改为<

 

下面请根据程序,测试数据与输出 及步骤图学习,跟着模拟一遍就会非常明白了

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-10;                                                              
struct Point{                                                                        
    double x,y;                                                                     
    int id;
    Point(double x=0,double y=0,int id=0):x(x),y(y),id(id){}
};
typedef Point Vector;

bool operator< (const Point&a,Point &b){
	return a.x<b.x ||(a.x==b.x && a.y<b.y);
}

Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double B){return Vector(A.x*B,A.y*B);}
Vector operator / (Vector A,double B){return Vector(A.x/B,A.y/B);}

int dcmp(double x){if(fabs(x)<eps)return 0;return (x>0)?1:-1;}
bool operator == (const Vector A,const Vector B){
    return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}  
double Length(Vector A){return sqrt(Dot(A,A));}         
double Cross(Vector A,Vector B){return A.x*B.y-B.x*A.y;}

int ConvexHell(Point p[],int n,Point ch[]) {
	sort(p,p+n);
	int m=0;
	for(int i=0;i<n;i++){
		while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0){           //下凸包 
			printf("update: segment %d TO %d destination: %d\n",m-2,m-1,i);
			m--;
		}
		ch[m++]=p[i];
		printf("ch[ %d ]= %d\n",m-1,i);
	}
	int k=m;
	for(int i=n-2;i>=0;i--){
		while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0){          //上凸包 
			printf("update: segment %d TO %d destination: %d\n",m-2,m-1,i);
			m--;
		}
		ch[m++]=p[i];
		printf("ch[ %d ]= %d\n",m-1,i);
	}
	if(n>1)m--;
	return m;
}
int main(){
	int n;
	scanf("%d",&n);
	Point p[20],ch[20];
	for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
	int sum=ConvexHell(p,n,ch);
	printf("Final Ans:\n");
	for(int i=0;i<sum;i++)printf("%.0lf %.0lf,",ch[i].x,ch[i].y);
    return 0;
}
/*测试数据及输出
8
3 0
4 8
5 5
6 3
6 7
8 4
9 2
10 7
ch[ 0 ]= 0
ch[ 1 ]= 1
update: segment 0 TO 1 destination: 2
ch[ 1 ]= 2
update: segment 0 TO 1 destination: 3
ch[ 1 ]= 3
ch[ 2 ]= 4
update: segment 1 TO 2 destination: 5
update: segment 0 TO 1 destination: 5
ch[ 1 ]= 5
update: segment 0 TO 1 destination: 6
ch[ 1 ]= 6
ch[ 2 ]= 7
ch[ 3 ]= 6
update: segment 2 TO 3 destination: 5
ch[ 3 ]= 5
update: segment 2 TO 3 destination: 4
ch[ 3 ]= 4
ch[ 4 ]= 3
update: segment 3 TO 4 destination: 2
ch[ 4 ]= 2
update: segment 3 TO 4 destination: 1
update: segment 2 TO 3 destination: 1
ch[ 3 ]= 1
ch[ 4 ]= 0
Final Ans:
3 0,9 2,10 7,4 8,
*/

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值