Skip to content

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

Closed

Conversation

ThijsRay
Copy link
Contributor

@ThijsRay ThijsRay commented Oct 7, 2018

This pull request aims to implement Graham Scan in Rust.

@june128 june128 added Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) labels Oct 7, 2018
@ThijsRay ThijsRay force-pushed the graham_scan_in_rust branch from 09d83b0 to 2cbaca7 Compare October 7, 2018 19:28
@ThijsRay ThijsRay changed the title WIP: Add Graham scan in Rust Add Graham scan in Rust Oct 7, 2018
sort_based_on_lowest_coordinate(&mut points);

// Take ownership of the pivot point
let pivot: Point = points.remove(0);
Copy link
Contributor

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)

@Butt4cak3
Copy link
Contributor

Don't forget to add code imports to the .md file.

@ThijsRay ThijsRay force-pushed the graham_scan_in_rust branch from 3a16a4f to 0b9c9c5 Compare October 9, 2018 14:51
@berquist berquist self-assigned this Apr 21, 2019
@berquist
Copy link
Member

berquist commented Oct 6, 2019

@ThijsRay If you do a rebase on top of current master, I'll review this.

@ThijsRay ThijsRay force-pushed the graham_scan_in_rust branch from 0b9c9c5 to 36ec677 Compare October 6, 2019 23:26
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.

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

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

Choose a reason for hiding this comment

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

Suggested change
(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());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.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 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} 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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(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.

Copy link
Member

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

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.

@berquist
Copy link
Member

@ThijsRay poke.

@leios leios added the lang: rust Rust programming language label Aug 28, 2021
@PeanutbutterWarrior
Copy link
Contributor

@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

PeanutbutterWarrior added a commit to PeanutbutterWarrior/algorithm-archive that referenced this pull request Dec 2, 2021
@ShadowMitia
Copy link
Contributor

Closing in favor of #964 based on this branch, thank you for your work!

ShadowMitia added a commit that referenced this pull request Jan 12, 2022
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) lang: rust Rust programming language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants