自己写的凸包模板

#include <iostream> #include <cmath> #include <algorithm> #define Max 501 #define eps 1e-8 using namespace std; struct point { double x; double y; double cross; }; point stack[Max];//储存凸包边缘顶点 point points[Max]; int NumOfVertex;//顶点数 int size;//stack长度 double sum;//边缘长度 bool cmpyx(point a,point b)//按照yx坐标排序 { if(a.y==b.y) return a.x<b.x; return a.y<b.y; } bool cmpcross(point a,point b)//按照角度大小排序 { if(a.cross == b.cross) return a.x<b.x; return a.cross<b.cross; } bool judge(point first,point second,point third)//判断是否往右拐 { double x1,x2,y1,y2,result; x1 = second.x-first.x; x2 = third.x-first.x; y1 = second.y-first.y; y2 = third.y-first.y; result = x1*y2-y1*x2; if(fabs(result)<eps) result = 0; if(result<=0)//往右拐 return true; return false; } void Cross()//计算极角 { int i; double x,y; for(i=1;i<NumOfVertex;i++) { x = (points[i].x-points[0].x); y = (points[i].y-points[0].y); points[i].cross = acos((x/(sqrt(x*x+y*y)))); } } void Graham()//找出凸包顶点 { int top=0,i; sort(points,points+NumOfVertex,cmpyx); Cross(); sort(points+1,points+NumOfVertex,cmpcross); stack[0] = points[0]; size = 1; for (i=1;i<NumOfVertex;i++) { while (size>1&&judge(stack[top-1],stack[top],points[i])) { size--; top--; } stack[++top] = points[i]; size++; } }

你可能感兴趣的:(自己写的凸包模板)