-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
jiegillet
merged 17 commits into
algorithm-archivists:master
from
wmboyles:wmboyles-GrahamScan
Jul 20, 2018
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
93648c6
Python implementation from Graham Scan
wmboyles 37d5cb3
Included Python code in .md
wmboyles 781e986
Compliance with pep8
wmboyles e944e1f
Updated .md to reflect code changes
wmboyles 494d083
fixed missing ":"
wmboyles 4839e22
Added full example to .md
wmboyles 132b0b0
Fixed incorrect code reference
wmboyles 5febc4b
fixed fatal typo
wmboyles 4c14dd4
Condensed sorting functions
wmboyles e4ee29c
Removed unneeded sorting function
wmboyles cd7778f
Updated .md to match code changes
wmboyles 5fa8fec
Fixed typo
wmboyles a3dada2
More pep8
wmboyles 388d3a6
Remove Duplicate Points
wmboyles 34a0f51
Driectory Change from .py file
wmboyles 675fd8e
Directory change for .md file
wmboyles 4f15bad
Merge branch 'master' into wmboyles-GrahamScan
jiegillet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
chapters/computational_geometry/gift_wrapping/graham_scan/code/python/grahamScan.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just: |
||
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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