Skip to content

Commit 3204369

Browse files
committed
Fixed PR issues and made the code a little more efficient
1 parent 3d1b8e2 commit 3204369

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

contents/jarvis_march/code/rust/jarvis_march.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

2-
// Define a point to be a tuple of two integers
32
type Point = (i64, i64);
43

54
// Is the turn counter clockwise?
65
fn turn_counter_clockwise(p1: Point, p2: Point, p3: Point) -> bool {
76
(p3.1 - p1.1) * (p2.0 - p1.0) >= (p2.1 - p1.1) * (p3.0 - p1.0)
87
}
98

10-
fn jarvis_march(gift: Vec<Point>) -> Option<Vec<Point>> {
9+
fn jarvis_march(gift: &[Point]) -> Option<Vec<Point>> {
1110
// There can only be a convex hull if there are more than 2 points
1211
if gift.len() < 3 {
1312
return None;
@@ -16,14 +15,14 @@ fn jarvis_march(gift: Vec<Point>) -> Option<Vec<Point>> {
1615
let leftmost_point = gift
1716
// Iterate over all points
1817
.iter()
19-
.cloned()
2018
// Find the point with minimum x
2119
.min_by_key(|i| i.0)
2220
// If there are no points in the gift, there might
2321
// not be a minimum. Unwrap fails (panics) the program
2422
// if there wasn't a minimum, but we know there always
2523
// is because we checked the size of the gift.
26-
.unwrap();
24+
.unwrap()
25+
.clone();
2726

2827
let mut hull = vec![leftmost_point];
2928

@@ -59,7 +58,7 @@ fn main() {
5958
(7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)
6059
];
6160

62-
let hull = jarvis_march(test_gift);
61+
let hull = jarvis_march(&test_gift);
6362

6463
println!("The points in the hull are: {:?}", hull);
6564
}

0 commit comments

Comments
 (0)