-
-
Notifications
You must be signed in to change notification settings - Fork 359
Add Graham scan in Rust #479
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
09d83b0
to
2cbaca7
Compare
sort_based_on_lowest_coordinate(&mut points); | ||
|
||
// Take ownership of the pivot point | ||
let pivot: Point = points.remove(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.
You usually don't state the types explicitly in Rust, especially when they're clear from the context (which in this case they are)
Don't forget to add code imports to the .md file. |
3a16a4f
to
0b9c9c5
Compare
@ThijsRay If you do a rebase on top of current master, I'll review this. |
0b9c9c5
to
36ec677
Compare
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.
Please remove the compiled binary from version control.
Point { x: 0.0, y: 2.0 }, | ||
Point { x: 2.0, y: 2.0 }, | ||
Point { x: 3.0, y: 4.0 }, | ||
Point { x: 3.0, y: 1.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.
Can you change these points to be the ones from the Julia example?
|
||
// Calculate the polar angle of a point relative to a reference point. | ||
fn polar_angle(reference: &Point, point: &Point) -> f64 { | ||
(point.y - point.y).atan2(point.x - reference.x) |
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.
(point.y - point.y).atan2(point.x - reference.x) | |
(point.y - reference.y).atan2(point.x - reference.x) |
|
||
// Sort all points based on the angle between the pivot point and itself | ||
&mut points | ||
.sort_by(|a, b| (polar_angle(a, &pivot).partial_cmp(&polar_angle(b, &pivot))).unwrap()); |
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.
.sort_by(|a, b| (polar_angle(a, &pivot).partial_cmp(&polar_angle(b, &pivot))).unwrap()); | |
.sort_by(|a, b| (polar_angle(&pivot, a).partial_cmp(&polar_angle(&pivot, b))).unwrap()); |
for consistency with the polar_angle
arguments.
while counter_clockwise(&points[m - 1], &points[m], &points[i]) { | ||
if m > 1 { | ||
m -= 1; | ||
} else if m == i { |
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.
} else if m == i { | |
} else if i == n { |
|
||
// Check if the turn of the points is counter clockwise. | ||
fn counter_clockwise(a: &Point, b: &Point, c: &Point) -> bool { | ||
(b.x - a.x) * (c.y - a.y) >= (b.y - a.y) * (c.x - a.x) |
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.
(b.x - a.x) * (c.y - a.y) >= (b.y - a.y) * (c.x - a.x) | |
(b.x - a.x) * (c.y - a.y) <= (b.y - a.y) * (c.x - a.x) |
if you want this function to return a bool.
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 missed something in my previous review, which is that in order to be consistent with the text (https://www.algorithm-archive.org/contents/graham_scan/graham_scan.html),
If the output of this function is 0, the points are collinear. If the output is positive, then the points form a counter-clockwise "left" turn. If the output is negative, then the points form a clockwise "right" turn. We basically do not want clockwise rotations, because this means we are at an interior angle.
this should return an integer.
|
||
// Check if the turn of the points is counter clockwise. | ||
fn counter_clockwise(a: &Point, b: &Point, c: &Point) -> bool { | ||
(b.x - a.x) * (c.y - a.y) >= (b.y - a.y) * (c.x - a.x) |
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 missed something in my previous review, which is that in order to be consistent with the text (https://www.algorithm-archive.org/contents/graham_scan/graham_scan.html),
If the output of this function is 0, the points are collinear. If the output is positive, then the points form a counter-clockwise "left" turn. If the output is negative, then the points form a clockwise "right" turn. We basically do not want clockwise rotations, because this means we are at an interior angle.
this should return an integer.
@ThijsRay poke. |
@ThijsRay I'm going to branch off this one and finish it off. I'll credit you of course. If you want to finish it yourself let me know and I'll leave you to it |
Closing in favor of #964 based on this branch, thank you for your work! |
* Implement Graham Scan in Rust * Add Thijs * Remove compiled binary * Define ordering of points for sorting * Clean up code * Change points to match other implementations * Apply requested changes from #479 * Fix function signature * Add function comment * Change algorithm to simpler version from python * Fix points to match other languages * Adjust line numbers for rust * Apply clippy suggestions Co-authored-by: Thijs Raymakers <[email protected]> Co-authored-by: Sammy Plat <[email protected]> Co-authored-by: Dimitri Belopopsky <[email protected]>
This pull request aims to implement Graham Scan in Rust.