# 2次元点群PointGroup pg
grahamScan(pg):
Stack st
leftmost ← pg.points の最も左下の点の番号
orderedIndex ← pg.points をleftmost を基準に偏角でソートしたインデックスの列
st.push(orderedIndex[0])
st.push(orderedIndex[1])
st.push(orderedIndex[2])
for i ← 3 to pg.N-1:
head = orderedIndex[i]
while st.size() ≥ 2:
top2 ← stの頂点の下の値
top ← stの頂点の値
if pg.points[top2] とpg.points[top]がなす直線に対して
pg.points[head]が右側にある(時計回り):
st.pop()
else:
break
st.push(head)