Skip to content

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

Merged
merged 13 commits into from
Jul 8, 2018
Merged

Python implementation for Jarvis March #223

merged 13 commits into from
Jul 8, 2018

Conversation

wmboyles
Copy link
Contributor

@wmboyles wmboyles commented Jul 6, 2018

I struggled a bit, so some of the individual commits are unnecessary.

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 6, 2018


#Find the leftmost point in the list
def leftmost(gift):
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 use a key= with min to reduce this function to one line: min(gift, key=lambda point: point[0])

Copy link
Member

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):
Copy link
Member

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
Copy link
Member

@jiegillet jiegillet Jul 7, 2018

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.

@jiegillet
Copy link
Member

hi @wmboyles, welcome to the AAA and thanks for your contribution.
On top of my other comments in text, could you add a main() function that actually runs on a set of points?

@jiegillet jiegillet self-assigned this Jul 7, 2018
@wmboyles
Copy link
Contributor Author

wmboyles commented Jul 7, 2018

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
Copy link
Member

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):
Copy link
Member

@jiegillet jiegillet Jul 7, 2018

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?

@jiegillet
Copy link
Member

jiegillet commented Jul 7, 2018

Thanks for the quick answer! Your changes are great.

  • I think the starting point should only be there once. We don't need to emphasize that a hull is a closed shape, it's the definition.
  • I left a couple of small comments.
  • Could you remove your commit on the Euclidian Algorithm? It gets really messy when a PR has more than one topic. Feel free to open a new PR about that if you want.
  • You need to in include your file into chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md. You can mimic how the other languages do it.
  • Small detail, but your file name is jarvits.py. Could you change it to jarvis.py or jarvisMarch.py?
  • Is it your first contribution? If so you can add you name into CONTRIBUTORS.md (at the root of the repo)

@wmboyles
Copy link
Contributor Author

wmboyles commented Jul 7, 2018

Changes made. I added myself as a contributor and changed the .md file in a separate pull requests.

@jiegillet
Copy link
Member

jiegillet commented Jul 8, 2018

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 :)
Get rid of any change toeuclidean_example.py and I'll merge directly.

Edit: also add the changes to jarvis_march.md and CONTRIBUTORS.md here.

@wmboyles
Copy link
Contributor Author

wmboyles commented Jul 8, 2018

Hopefully that should fix all of my mistakes.

Copy link
Member

@jiegillet jiegillet left a 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!

@jiegillet jiegillet merged commit b1d367c into algorithm-archivists:master Jul 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants