File tree Expand file tree Collapse file tree 1 file changed +6
-8
lines changed
chapters/computational_geometry/gift_wrapping/graham_scan/code/c Expand file tree Collapse file tree 1 file changed +6
-8
lines changed Original file line number Diff line number Diff line change 1
1
#include <math.h>
2
+ #include <stdbool.h>
2
3
#include <stddef.h>
3
4
#include <stdio.h>
4
5
#include <stdlib.h>
@@ -9,20 +10,17 @@ struct point {
9
10
};
10
11
11
12
int cmp_points (const void * a , const void * b ) {
12
- struct point point1 = * (struct point * )a ;
13
- struct point point2 = * (struct point * )b ;
14
-
15
- if (point1 .y > point2 .y ) {
13
+ if (((struct point * )a )-> y > ((struct point * )b )-> y ) {
16
14
return 1 ;
17
- } else if (point1 . y < point2 . y ) {
15
+ } else if ((( struct point * ) a ) -> y < (( struct point * ) b ) -> y ) {
18
16
return -1 ;
19
17
} else {
20
18
return 0 ;
21
19
}
22
20
}
23
21
24
- double ccw (struct point a , struct point b , struct point c ) {
25
- return (b .x - a .x )* (c .y - a .y ) - (b .y - a .y )* (c .x - a .x );
22
+ bool is_left_of (struct point a , struct point b , struct point c ) {
23
+ return (b .x - a .x ) * (c .y - a .y ) < (b .y - a .y ) * (c .x - a .x );
26
24
}
27
25
28
26
double polar_angle (struct point origin , struct point p ) {
@@ -72,7 +70,7 @@ size_t graham_scan(struct point *points, size_t size) {
72
70
73
71
size_t m = 1 ;
74
72
for (size_t i = 2 ; i <= size ; ++ i ) {
75
- while (ccw (tmp_points [m - 1 ], tmp_points [m ], tmp_points [i ]) <= 0 ) {
73
+ while (is_left_of (tmp_points [m - 1 ], tmp_points [m ], tmp_points [i ])) {
76
74
if (m > 1 ) {
77
75
m -- ;
78
76
continue ;
You can’t perform that action at this time.
0 commit comments