MCPcopy Create free account
hub / github.com/algorithm-archivists/algorithm-archive / graham_scan

Function graham_scan

contents/graham_scan/code/c/graham.c:65–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63}
64
65size_t graham_scan(struct point *points, size_t size) {
66 qsort(points, size, sizeof(struct point), cmp_points);
67 polar_angles_sort(points, points[0], size);
68
69 struct point tmp_points[size + 1];
70 memcpy(tmp_points + 1, points, size * sizeof(struct point));
71 tmp_points[0] = tmp_points[size];
72
73 size_t m = 1;
74 for (size_t i = 2; i <= size; ++i) {
75 while (ccw(tmp_points[m - 1], tmp_points[m], tmp_points[i]) <= 0) {
76 if (m > 1) {
77 m--;
78 continue;
79 } else if (i == size) {
80 break;
81 } else {
82 i++;
83 }
84 }
85
86 m++;
87 struct point tmp = tmp_points[i];
88 tmp_points[i] = tmp_points[m];
89 tmp_points[m] = tmp;
90 }
91
92 memcpy(points, tmp_points + 1, size * sizeof(struct point));
93
94 return m;
95}
96
97int main() {
98 struct point points[] = {{-5, 2}, {5, 7}, {-6, -12}, {-14, -14}, {9, 9},

Callers 1

mainFunction · 0.70

Calls 2

polar_angles_sortFunction · 0.85
ccwFunction · 0.70

Tested by

no test coverage detected