Skip to content

Commit 5d1b24d

Browse files
committed
Add comments to Graham Scan in Rust
1 parent ea8ae6b commit 5d1b24d

File tree

1 file changed

+6
-3
lines changed
  • contents/graham_scan/code/rust/src

1 file changed

+6
-3
lines changed

contents/graham_scan/code/rust/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ struct Point {
44
y: f64,
55
}
66

7+
// Check if the turn of the points is counter clockwise.
78
fn counter_clockwise(a: &Point, b: &Point, c: &Point) -> bool {
89
((b.x - a.x) * (c.y - a.y)) >= ((b.y - a.y) * (c.x - a.x))
910
}
1011

12+
// Calculate the polar angle of a point relative to a reference point.
1113
fn polar_angle(reference: &Point, point: &Point) -> f64 {
1214
(point.y - point.y).atan2(point.x - reference.x)
1315
}
@@ -29,6 +31,7 @@ fn graham_scan(mut points: Vec<Point>) -> Vec<Point> {
2931
let n = points.len();
3032
let mut m = 1;
3133

34+
// Move the points of the hull towards the beginning of the vector.
3235
for mut i in 2..n {
3336
while counter_clockwise(&points[m - 1], &points[m], &points[i]) {
3437
if m > 1 {
@@ -44,6 +47,7 @@ fn graham_scan(mut points: Vec<Point>) -> Vec<Point> {
4447
points.swap(i, m);
4548
}
4649

50+
// Remove all non-hull points from the vector
4751
points.truncate(m + 2);
4852
points
4953
}
@@ -65,7 +69,6 @@ fn main() {
6569
Point { x: 3.0, y: 1.0 },
6670
];
6771

68-
//println!("{:#?}", points);
69-
let points = graham_scan(points);
70-
println!("{:#?}", points);
72+
let hull_points = graham_scan(points);
73+
println!("{:#?}", hull_points);
7174
}

0 commit comments

Comments
 (0)