Skip to content

Python implementation for Graham Scan #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from math import atan2

#Is the turn counter clockwise?
def CCW(p1, p2, p3):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incosistent naming, see my main main comment

return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0])


#Find the point with least y-value. If tie, use point with least x-value.
def minYPoint(points):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just: min(points, key=lambda p: (p[1], p[0]));

minYPt = points[0]

for point in points[1:]:
if point[1] < minYPt[1]:
minYPt = point
elif point[1] == minYPt[1] and point[0]<minYPt[0]:
minYPt = point

return minYPt


#Find the polar angle of each point in list relative to a reference point
def polarAngles(ref, points):
return [atan2(point[1]-ref[0],point[0]-ref[0]) for point in points]


#Sort the list of point by their polar angle
def sortByPolar(ref, points):
return [x for _,x in sorted(zip(polarAngles(ref,points),points))]


def grahamScan(gift):
start = minYPoint(gift) #Must be in hull
gift.remove(start)

S = sortByPolar(start,gift)
hull = [start,S[0],S[1]]

#Remove points from hull that make the hull concave
for pt in S[2:]:
while not CCW(hull[-2],hull[-1],pt):
del hull[-1]
hull.append(pt)

return hull


def main():
testGift = [(-5,2),(5,7),(-6,-12),(-14,-14),(9,9),
(-1,-1),(-10,11),(-6,15),(-6,-8),(15,-9),
(7,-7),(-2,-9),(6,-5),(0,14),(2,8)]
hull = grahamScan(testGift)

print("The points in the hull are:")
for point in hull:
print(point)

main()
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
[import:24-26, lang:"c_cpp"](code/c/graham.c)
{% sample lang="js" %}
[import:36-38, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import3-5, lang:"python"](code/python/grahamScan.py)
{% endmethod %}

If the output of this function is 0, the points are collinear.
Expand All @@ -42,6 +44,8 @@ In the end, the code should look something like this:
[import:65-95, lang:"c_cpp"](code/c/graham.c)
{% sample lang="js" %}
[import:1-30, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import31-44, lang:"python"](code/python/grahamScan.py)
{% endmethod %}

### Bibliography
Expand Down