Convex Package-graham Scanning method

Source: Internet
Author: User

Rt,graham scanning method to find convex hull is divided into 3 steps:

1. Find the smallest point in Y

2. With the smallest point o of Y as the origin, all remaining points are relative to the angle of O and are ordered from small to large at the polar angle.

3. For the sorted point set, with the stack, complete the Graham scan.

convexhull.py

#coding =utf-8import mathimport numpyimport Pylab as pl# draw original Def drawgraph (x, y): Pl.title ("The Convex Hull") Pl.xlabel ("x axis") Pl.ylabel ("Y axis") pl.plot (x, y, ' ro ') #画凸包def drawch (x, y): #pl. Plot (x,y1,label= ' DP ', linewidth=4,color= ' Red ') Pl.plot (x,y,color= ' Blue ', linewidth=2) Pl.plot (x[-1],y[-1],x[0],y[0]) lastx=[x[-1],x[0]] Lasty=[y[-1],y[0 ]] Pl.plot (lastx,lasty,color= ' Blue ', linewidth=2) #存点的矩阵, one point per line, column 0->x coordinates, column 1->y coordinates, column 2-> represents the Polar def matrix (rows, cols): cols=3 mat = [[0 for Col. Range (cols)] for row in range (rows)] return mat# return cross product def Crossmut (STACK,P3): P2=stack[-1] p1=stack[-2] vx,vy= (p2[0]-p1[0],p2[1]-p1[1]) wx,wy= (p3[0]-p1[0],p3[1]-p1[1]) retur N (vx*wy-vy*wx) #Graham扫描法O (NLOGN), Mat is a polar-sorted point set def grahamscan (MAT): #print Mat Points=len (MAT) #点数 "" "For K I N Range (points): print mat[k][0],mat[k][1],mat[k][2] "" "Stack=[" Stack.append ((mat[0][0],mat[0][1))) #pus H P0 Stack.append (mat[1][0],mAT[1][1])) #push P1 stack.append ((mat[2][0],mat[2][1)) #push P2 for I in Range (3,points): #print stack P3= (Mat[i][0],mat[i][1]) while Crossmut (STACK,P3) <0:stack.pop () stack.append (p3) return stack #ma Inmax_num = #最大点数x =100*numpy.random.random (max_num) #[0,100) y=100*numpy.random.random (max_num) drawGraph (x, Y) mat = Matrix (max_num,3) #最多能存50个点minn = 300for i in range (max_num): #存点集 mat[i][0],mat[i][1]=x[i],y[i] If y[i]<min N: # (x[tmp],y[tmp])-->y axis Minimum coordinate minn=y[i] tmp = ID = {} #利用字典的排序for i in range (max_num): #计算极角 if (ma T[I][0],MAT[I][1]) = = (X[tmp],y[tmp]): Mat[i][2]=0 else:mat[i][2]=math.atan2 ((Mat[i][1]-y[tmp]), (Mat[i][0]-x[tmp]) ) d[(mat[i][0],mat[i][1])]=mat[i][2]lst=sorted (D.items (), Key=lambda e:e[1]) #按极角由小到大排序for i in Range (max_num): #装排序后 Matrix ((x, y), eth0) =lst[i] Mat[i][0],mat[i][1],mat[i][2]=x,y,eth0stack=grahamscan (MAT) x,y=[],[]for Z in Stack:x.app End (Z[0]) y.append (z[1]) drawch (x, y) pl. Show () 
Ps:graham's complexity is O (NLOGN)

To find the furthest point of the plane can be converted to the first convex hull + rotation jam (it can be proved that the most distant point on the plane must be on the convex package.) )

Convex Package-graham Scanning method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.