Skip to content

Commit 61bde39

Browse files
committed
maded small changes to graham.c
1 parent a22414e commit 61bde39

File tree

1 file changed

+6
-8
lines changed
  • chapters/computational_geometry/gift_wrapping/graham_scan/code/c

1 file changed

+6
-8
lines changed

chapters/computational_geometry/gift_wrapping/graham_scan/code/c/graham.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <math.h>
2+
#include <stdbool.h>
23
#include <stddef.h>
34
#include <stdio.h>
45
#include <stdlib.h>
@@ -9,20 +10,17 @@ struct point {
910
};
1011

1112
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) {
1614
return 1;
17-
} else if (point1.y < point2.y) {
15+
} else if (((struct point*)a)->y < ((struct point*)b)->y) {
1816
return -1;
1917
} else {
2018
return 0;
2119
}
2220
}
2321

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);
2624
}
2725

2826
double polar_angle(struct point origin, struct point p) {
@@ -72,7 +70,7 @@ size_t graham_scan(struct point *points, size_t size) {
7270

7371
size_t m = 1;
7472
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])) {
7674
if (m > 1) {
7775
m--;
7876
continue;

0 commit comments

Comments
 (0)