@@ -4,10 +4,12 @@ struct Point {
4
4
y : f64 ,
5
5
}
6
6
7
+ // Check if the turn of the points is counter clockwise.
7
8
fn counter_clockwise ( a : & Point , b : & Point , c : & Point ) -> bool {
8
9
( ( b. x - a. x ) * ( c. y - a. y ) ) >= ( ( b. y - a. y ) * ( c. x - a. x ) )
9
10
}
10
11
12
+ // Calculate the polar angle of a point relative to a reference point.
11
13
fn polar_angle ( reference : & Point , point : & Point ) -> f64 {
12
14
( point. y - point. y ) . atan2 ( point. x - reference. x )
13
15
}
@@ -29,6 +31,7 @@ fn graham_scan(mut points: Vec<Point>) -> Vec<Point> {
29
31
let n = points. len ( ) ;
30
32
let mut m = 1 ;
31
33
34
+ // Move the points of the hull towards the beginning of the vector.
32
35
for mut i in 2 ..n {
33
36
while counter_clockwise ( & points[ m - 1 ] , & points[ m] , & points[ i] ) {
34
37
if m > 1 {
@@ -44,6 +47,7 @@ fn graham_scan(mut points: Vec<Point>) -> Vec<Point> {
44
47
points. swap ( i, m) ;
45
48
}
46
49
50
+ // Remove all non-hull points from the vector
47
51
points. truncate ( m + 2 ) ;
48
52
points
49
53
}
@@ -65,7 +69,6 @@ fn main() {
65
69
Point { x: 3.0 , y: 1.0 } ,
66
70
] ;
67
71
68
- //println!("{:#?}", points);
69
- let points = graham_scan ( points) ;
70
- println ! ( "{:#?}" , points) ;
72
+ let hull_points = graham_scan ( points) ;
73
+ println ! ( "{:#?}" , hull_points) ;
71
74
}
0 commit comments