-
-
Notifications
You must be signed in to change notification settings - Fork 360
Python implementation for Jarvis March #223
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
Conversation
Added python3 example for Jarvits March.
Create jarvits.py
|
||
|
||
#Find the leftmost point in the list | ||
def leftmost(gift): |
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.
You can use a key=
with min
to reduce this function to one line: min(gift, key=lambda point: point[0])
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.
Well, actually, in this case you can just use min(gift)
directly, because when comparing a list, python compares by the first element first.
|
||
|
||
#Are two points the same point? | ||
def equal(p1,p2): |
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.
Can't you simply use p1==p2
?
|
||
def jarvisMarch(gift): | ||
n = len(gift) #Number of points in list | ||
hull = [(None,None)] * n #The hull, initially empty points |
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.
I know that initializing lists is a good idea in general, but here it looks clunky (especially later when you remove the empty points). I would just append points to an initially empty hull as they come.
hi @wmboyles, welcome to the AAA and thanks for your contribution. |
Thanks for the input @jiegillet . I have implemented everything you asked for. Do you think the hull should include the starting point both at the beginning and end of the hull? I think having it emphasizes the fact that the convex hull is a closed shape, but understand the possible confusion of having the same point in the hull twice. |
def jarvisMarch(gift): | ||
n = len(gift) #Number of points in list | ||
pointOnHull = min(gift) #leftmost point in gift | ||
hull = [pointOnHull] #leftmost point guranteed to be in hull |
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.
tiny typo guranteed
while True: | ||
endpoint = gift[0] | ||
for j in range(1,n): | ||
if endpoint==pointOnHull or not CCW(gift[j],hull[i],endpoint): |
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.
hull[i]
is the only point where you use i
, correct? Could you get away with using hull[-1]
and drop i
altogether?
Thanks for the quick answer! Your changes are great.
|
Changes made. I added myself as a contributor and changed the .md file in a separate pull requests. |
It's almost done, but during your PR, the euclidian algorithm has changed, and here you are still modifying it. That's exactly why you shouldn't mix PRs :) Edit: also add the changes to |
Hopefully that should fix all of my mistakes. |
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.
Looks good, thanks a lot!
I struggled a bit, so some of the individual commits are unnecessary.