Skip to content

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
merged 3 commits into from
Jul 11, 2020

Conversation

jdonszelmann
Copy link
Contributor

No description provided.

@jdonszelmann jdonszelmann changed the title Added rust implementation of the jarvis march algorithm Rust implementation of the jarvis march algorithm Jul 4, 2020
Added rust implementation of the jarvis march algorithm
@berquist berquist added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 4, 2020
Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Just some minor things.

(p3.1 - p1.1) * (p2.0 - p1.0) >= (p2.1 - p1.1) * (p3.0 - p1.0)
}

fn jarvis_march(gift: Vec<Point>) -> Option<Vec<Point>> {
Copy link
Member

Choose a reason for hiding this comment

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

Nice idea to use an option instead of returning an empty vec, which means something other than "couldn't make the hull". Do you need to take ownership of gift though?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not necessarily, good point and will be changed :)

@@ -0,0 +1,65 @@

// Define a point to be a tuple of two integers
Copy link
Member

Choose a reason for hiding this comment

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

This is an obvious comment that doesn't add much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll remove it


// 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)
Copy link
Contributor Author

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?

Copy link
Contributor Author

@jdonszelmann jdonszelmann Jul 5, 2020

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 )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or maybe an enum?

Copy link
Member

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:

  • C
  • Haskell
  • Java

and these implementations have a function that returns a boolean:

  • C#
  • C++ (inline lambda)
  • Common Lisp
  • Go
  • JavaScript
  • Python
  • Vlang

and these implementations do something else:

  • Julia

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

  1. < 0, == 0, or > 0 for two points, or
  2. true/false for some condition, say 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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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)

Copy link
Member

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 over Signed + Float, that's up to you.

If you can write a clean version that uses Ordering, that would be cool.

Copy link
Contributor Author

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

Copy link
Contributor Author

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:

    let leftmost_point = gift
        // Iterate over all points
        .iter()
        // Find the point with minimum x
        // Unfortunately, we can't use min_by_key here because floats in rust
        // do not implement Ord.
        .fold((f64::NAN, f64::NAN), |acc, val| if acc.0.partial_cmp(&val.0) == Some(Ordering::Less){
            acc
        } else {
            val
        })
        // 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();

@berquist berquist merged commit b4b5071 into algorithm-archivists:master Jul 11, 2020
@jdonszelmann
Copy link
Contributor Author

🎉

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.

2 participants