Skip to content

Commit 3d1b8e2

Browse files
committed
Added rust implementation of the jarvis march algorithm
Added rust implementation of the jarvis march algorithm
1 parent 3005e35 commit 3d1b8e2

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
5151
- Vincent Zalzal
5252
- Jonathan D B Van Schenck
5353
- James Goytia
54+
- Jonathan Dönszelmann
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
// Define a point to be a tuple of two integers
3+
type Point = (i64, i64);
4+
5+
// Is the turn counter clockwise?
6+
fn turn_counter_clockwise(p1: Point, p2: Point, p3: Point) -> bool {
7+
(p3.1 - p1.1) * (p2.0 - p1.0) >= (p2.1 - p1.1) * (p3.0 - p1.0)
8+
}
9+
10+
fn jarvis_march(gift: Vec<Point>) -> Option<Vec<Point>> {
11+
// There can only be a convex hull if there are more than 2 points
12+
if gift.len() < 3 {
13+
return None;
14+
}
15+
16+
let leftmost_point = gift
17+
// Iterate over all points
18+
.iter()
19+
.cloned()
20+
// Find the point with minimum x
21+
.min_by_key(|i| i.0)
22+
// If there are no points in the gift, there might
23+
// not be a minimum. Unwrap fails (panics) the program
24+
// if there wasn't a minimum, but we know there always
25+
// is because we checked the size of the gift.
26+
.unwrap();
27+
28+
let mut hull = vec![leftmost_point];
29+
30+
let mut point_on_hull = leftmost_point;
31+
loop {
32+
// Search for the next point on the hull
33+
let mut endpoint = gift[0];
34+
for i in 1..gift.len() {
35+
if endpoint == point_on_hull || !turn_counter_clockwise(gift[i], hull[hull.len() - 1], endpoint) {
36+
endpoint = gift[i];
37+
}
38+
}
39+
40+
point_on_hull = endpoint;
41+
42+
// Stop whenever we got back to the same point
43+
// as we started with, and we wrapped the gift
44+
// completely.
45+
if hull[0] == endpoint {
46+
break;
47+
} else {
48+
hull.push(point_on_hull);
49+
}
50+
}
51+
52+
Some(hull)
53+
}
54+
55+
fn main() {
56+
let test_gift = vec![
57+
(-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9),
58+
(-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9),
59+
(7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)
60+
];
61+
62+
let hull = jarvis_march(test_gift);
63+
64+
println!("The points in the hull are: {:?}", hull);
65+
}

contents/jarvis_march/jarvis_march.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Since this algorithm, there have been many other algorithms that have advanced t
4848
[import, lang:"go"](code/golang/jarvis.go)
4949
{% sample lang="v" %}
5050
[import, lang:"v"](code/v/jarvis.v)
51+
{% sample lang="rust" %}
52+
[import, lang:"rust"](code/rust/jarvis_march.rs)
5153
{% endmethod %}
5254

5355
<script>

0 commit comments

Comments
 (0)