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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added contents/graham_scan/code/rust/graham_scan
Binary file not shown.
73 changes: 73 additions & 0 deletions contents/graham_scan/code/rust/graham_scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#[derive(Debug)]
struct Point {
x: f64,
y: f64,
}

// 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.

}

// 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)

}

fn graham_scan(mut points: Vec<Point>) -> Vec<Point> {
// First, sort the points so the one with the lowest y-coordinate comes first (the pivot)
sort_based_on_lowest_coordinate(&mut points);

// Take ownership of the pivot point
let pivot = points.remove(0);

// 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.


// Reinsert the pivot point
points.insert(0, pivot);

let n = points.len();
let mut m = 1;

// Move the points of the hull towards the beginning of the vector.
for mut i in 2..n {
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 {

break;
} else {
i += 1;
}
}

m += 1;
points.swap(i, m);
}

// Remove all non-hull points from the vector
points.truncate(m + 1);
points
}

fn sort_based_on_lowest_coordinate(points: &mut Vec<Point>) {
points.sort_unstable_by(|a, b| (a.y).partial_cmp(&b.y).unwrap());
}

fn main() {
let points = vec![
Point { x: 1.0, y: 3.0 },
Point { x: 2.0, y: 4.0 },
Point { x: 4.0, y: 0.0 },
Point { x: 1.0, y: 0.0 },
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?

];

let hull_points = graham_scan(points);
println!("{:#?}", hull_points);
}
6 changes: 6 additions & 0 deletions contents/graham_scan/graham_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
[import:27-29, lang:"java"](code/java/GrahamScan.java)
{% sample lang="cpp" %}
[import:10-12, lang="cpp"](code/c++/graham_scan.cpp)
{% sample lang="rs" %}
[import:7-10, lang: "rust"](code/rust/graham_scan.rs)
{% endmethod %}

If the output of this function is 0, the points are collinear.
Expand Down Expand Up @@ -58,6 +60,8 @@ In the end, the code should look something like this:
[import:35-70, lang:"java"](code/java/GrahamScan.java)
{% sample lang="cpp" %}
[import:14-47, lang="cpp"](code/c++/graham_scan.cpp)
{% sample lang="rs" %}
[import:17-53, lang: "rust"](code/rust/graham_scan.rs)
{% endmethod %}

### Bibliography
Expand All @@ -83,6 +87,8 @@ In the end, the code should look something like this:
[import, lang:"java"](code/java/GrahamScan.java)
{% sample lang="cpp" %}
[import, lang="cpp"](code/c++/graham_scan.cpp)
{% sample lang="rs" %}
[import, lang: "rust"](code/rust/graham_scan.rs)
{% endmethod %}

<script>
Expand Down