-
-
Notifications
You must be signed in to change notification settings - Fork 359
Rust implementation of the jarvis march algorithm #723
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
berquist
merged 3 commits into
algorithm-archivists:master
from
jdonszelmann:rust_jarvis
Jul 11, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,64 @@ | ||
|
||
type Point = (i64, i64); | ||
|
||
// Is the turn counter clockwise? | ||
fn turn_counter_clockwise(p1: Point, p2: Point, p3: Point) -> bool { | ||
(p3.1 - p1.1) * (p2.0 - p1.0) >= (p2.1 - p1.1) * (p3.0 - p1.0) | ||
} | ||
|
||
fn jarvis_march(gift: &[Point]) -> Option<Vec<Point>> { | ||
// There can only be a convex hull if there are more than 2 points | ||
if gift.len() < 3 { | ||
return None; | ||
} | ||
|
||
let leftmost_point = gift | ||
// Iterate over all points | ||
.iter() | ||
// Find the point with minimum x | ||
.min_by_key(|i| i.0) | ||
// If there are no points in the gift, there might | ||
// not be a minimum. Unwrap fails (panics) the program | ||
// if there wasn't a minimum, but we know there always | ||
// is because we checked the size of the gift. | ||
.unwrap() | ||
.clone(); | ||
|
||
let mut hull = vec![leftmost_point]; | ||
|
||
let mut point_on_hull = leftmost_point; | ||
loop { | ||
// Search for the next point on the hull | ||
let mut endpoint = gift[0]; | ||
for i in 1..gift.len() { | ||
if endpoint == point_on_hull || !turn_counter_clockwise(gift[i], hull[hull.len() - 1], endpoint) { | ||
endpoint = gift[i]; | ||
} | ||
} | ||
|
||
point_on_hull = endpoint; | ||
|
||
// Stop whenever we got back to the same point | ||
// as we started with, and we wrapped the gift | ||
// completely. | ||
if hull[0] == endpoint { | ||
break; | ||
} else { | ||
hull.push(point_on_hull); | ||
} | ||
} | ||
|
||
Some(hull) | ||
} | ||
|
||
fn main() { | ||
let test_gift = vec![ | ||
(-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) | ||
]; | ||
|
||
let hull = jarvis_march(&test_gift); | ||
|
||
println!("The points in the hull are: {:?}", hull); | ||
} |
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.
@berquist I saw that in another implementation of Jarvis (I believe maybe Julia?) someone wanted this function to return an integer because the text says so. Should I do this in rust too?
Uh oh!
There was an error while loading. Please reload this page.
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 remember, it was in the Graham scan in rust (#479 )
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.
Or maybe an enum?
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.
For Jarvis March, these implementations have a function that returns a float:
and these implementations have a function that returns a boolean:
and these implementations do something else:
As for which should be used, being consistent is best. I made that comment about returning a number in Graham Scan because of what the text describes, but here there is no such description in the text, despite this part of the algorithm being the same.
Regarding the enum, what advantage would that bring? This is really about getting an ordering between points, which you represent either as
p1 <= p2
.My response is that either a bool or a float is fine, and if we decide to standardize later on one or the other, it's an easy change.
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.
Regarding floats, some algorithms (including this one, but also c# and V) can't even deal with floating point input coordinates. Any thoughts about that?
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.
And for the enum, in rust it's not uncommon to use an Ordering as a return type here (https://doc.rust-lang.org/stable/std/cmp/enum.Ordering.html)
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.
Ideally the allowed input is any number type (maybe not complex), but if it's restricted to just ints or floats, it's fine as long as the code is correct. If you want to make
Point
generic overSigned + Float
, that's up to you.If you can write a clean version that uses
Ordering
, that would be cool.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.
Ordering won't be clean. I'll just leave it at a boolean. I tried to convert the code to allow for floats, but this really messes the clarity up a lot because in rust, floats don't implement Ord and Eq
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.
The part where I find the leftmost point would change from a simple min_by_key() to this: