def BruteForce(pointList): if len(pointList)<=3: return pointList #去重 pointList=list(set(pointList)) #固定A点 A = Tool.findIth(0,len(pointList)-1,pointList,1,lambda x,y:cmp(x[1],y[1])) pointList.remove(A) #枚举B,C,P while 1: dropList=[] for b in range(0,len(pointList)-1): for c in range(b+1,len(pointList)): for p in range(0,len(pointList)): if p==b or p==c: continue B=pointList[b] C=pointList[c] P=pointList[p] if Tool.isInTriangle(A,B,C,P)==1: dropList.append(P) if len(dropList)==0: break #去重 dropList=list(set(dropList)) for p in dropList: pointList.remove(p) pointList.append(A) return pointList
def Graham_Scan(pointlist): #构造有序list point = Tool.findIth(0,len(pointlist)-1,pointlist,1,comparePointX)#获取最左边点 uplist = [] downlist=[] otherlist=[] for w in pointlist: if w[0]==point[0] and w[1]==point[1]: continue elif w[0]==point[0] and w[1]>point[1]: uplist.append((w,'+')) elif w[0]==point[0] and w[1]<point[1]: downlist.append((w,'-')) else: otherlist.append((w,float(w[1]-point[1])/(w[0]-point[0]))) #排序 uplist.sort(cmp=lambda x,y:comparePointY(x[0],y[0]),reverse=True)#降序 downlist.sort(cmp=lambda x,y:comparePointY(x[0],y[0]))#升序 otherlist.sort(cmp=lambda x,y:cmp(x[1],y[1]))#按斜率升序 #合并为一个有序list list=[] list.append(point) for w in downlist: list.append(w[0]) for w in otherlist: list.append(w[0]) for w in uplist: list.append(w[0]) #Graham-Scan stack = [] if len(list)<3: return list for w in range(3): stack.append(list.pop(0)) for w in range(len(list)): next = list.pop(0) while isLeftMove(stack[-2],stack[-1],next,point)=='right': stack.pop() if len(stack)<=2: break stack.append(next) return stack