Skip to content

Implement Graham Scan in Rust #964

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
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Henrik Abel Christensen
- K. Shudipto Amin
- Peanutbutter_Warrior
- Thijs Raymakers
96 changes: 96 additions & 0 deletions contents/graham_scan/code/rust/graham_scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use std::cmp::Ordering;

#[derive(Debug, PartialEq)]
struct Point {
x: f64,
y: f64,
}

impl Eq for Point {}

impl PartialOrd for Point {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.y == other.y {
self.x.partial_cmp(&other.x)
} else {
self.y.partial_cmp(&other.y)
}
}
}

// Defines an order for Points so they can be sorted
impl Ord for Point {
fn cmp(&self, other: &Self) -> Ordering {
// Neither field of Point will be NaN, so this is safe
self.partial_cmp(other).unwrap()
}
}

fn counter_clockwise(a: &Point, b: &Point, c: &Point) -> f64 {
(b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
}

// Calculate the polar angle of a point relative to a reference point.
fn polar_angle(reference: &Point, point: &Point) -> f64 {
(point.y - reference.y).atan2(point.x - reference.x)
}

fn graham_scan(mut points: Vec<Point>) -> Vec<Point> {
points.sort_unstable();

let pivot = points.remove(0);

// Sort all points based on the angle between the pivot point and itself
points.sort_by(|a, b| (polar_angle(&pivot, a).
partial_cmp(&polar_angle(&pivot, b))
).unwrap()
);

points.insert(0, pivot);

let mut m = 1;

// Move the points of the hull towards the beginning of the vector.
for mut i in 2..points.len() {
while counter_clockwise(&points[m - 1], &points[m], &points[i]) <= 0.0 {
if m > 1 {
m -= 1;
// All points are colinear
} else if i == points.len() {
break;
} else {
i += 1;
}
}

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

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

fn main() {
let points = vec![
Point { x: -5.0, y: 2.0 },
Point { x: 5.0, y: 7.0 },
Point { x: -6.0, y: -12.0 },
Point { x: -14.0, y: -14.0 },
Point { x: 9.0, y: 9.0 },
Point { x: -1.0, y: -1.0 },
Point { x: -10.0, y: -11.0 },
Point { x: -6.0, y: 15.0 },
Point { x: -6.0, y: 8.0 },
Point { x: 15.0, y: -9.0 },
Point { x: -7.0, y: -7.0 },
Point { x: -2.0, y: -9.0 },
Point { x: 6.0, y: -5.0 },
Point { x: 0.0, y: 14.0 },
Point { x: 2.0, y: 8.0 },
];

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 @@ -32,6 +32,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
[import:18-20, lang="cpp"](code/cpp/graham_scan.cpp)
{% sample lang="coco" %}
[import:4-8, lang="coconut"](code/coconut/graham_scan.coco)
{% 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 @@ -66,6 +68,8 @@ In the end, the code should look something like this:
[import:26-62, lang="cpp"](code/cpp/graham_scan.cpp)
{% sample lang="coco" %}
[import:17-30, lang="coconut"](code/coconut/graham_scan.coco)
{% sample lang="rs" %}
[import:17-53, lang: "rust"](code/rust/graham_scan.rs)
{% endmethod %}

### Bibliography
Expand Down Expand Up @@ -95,6 +99,8 @@ In the end, the code should look something like this:
[import, lang="cpp"](code/cpp/graham_scan.cpp)
{%sample lang="coco" %}
[import, lang="coconut"](code/coconut/graham_scan.coco)
{% sample lang="rs" %}
[import, lang: "rust"](code/rust/graham_scan.rs)
{% endmethod %}

<script>
Expand Down