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 8 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,46 @@
from math import atan2


#Is the turn counter clockwise?
def counter_Clockwise(p1, p2, p3):
return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0])


#Find the polar angle of each point in list relative to a reference point
def polar_Angles(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 sort_By_Polar(ref, points):
Copy link
Member

Choose a reason for hiding this comment

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

You can replace your two sorting functions by sorted(ref, key=lambda point: atan2(point[1]-ref[0],point[0]-ref[0]) ), although defining the atan2 function to polar_angle might be more readable.

return [x for _,x in sorted(zip(polar_Angles(ref,points),points))]


def graham_Scan(gift):
start = min(gift, key=lambda p (p[1],p[0])) #Must be in hull
Copy link
Member

Choose a reason for hiding this comment

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

dude! You're missing the : this is still fatal! Have you tried running it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got it now. I don't use lambdas too often.

gift.remove(start)

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

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

return hull


def main():
test_Gift = [(-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 = graham_Scan(test_Gift)

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" %}
[import:5-6, 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" %}
[import:19-32, lang:"python"](code/python/grahamScan.py)
{% endmethod %}

### Bibliography
Expand All @@ -61,7 +65,11 @@ In the end, the code should look something like this:
### C
[import, lang:"c_cpp"](code/c/graham.c)
{% sample lang="js" %}
### Javascript
[import, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
### Python
[import, lang:"python"](code/python/grahamScan.py)
{% endmethod %}

<script>
Expand Down