Skip to content

Adding graham.c #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct point {
double x, y;
};

int cmp_points(const void *a, const void *b) {
struct point* pa = (struct point*) a;
struct point* pb = (struct point*) b;

if (pa->y > pb->y) {
return 1;
} else if (pa->y < pb->y) {
return -1;
} else {
return 0;
}
}

double ccw(struct point a, struct point b, struct point c) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think ccw is the best name here. That would make sense if it returned a bool. I'd probably call it orientation or something like that.

return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

double polar_angle(struct point origin, struct point p) {
return atan2(p.y - origin.y, p.x - origin.x);
}

void polar_angles_sort(struct point *points, struct point origin, size_t size) {
if (size < 2) {
return;
}

double pivot_angle = polar_angle(origin, points[size / 2]);

int i = 0;
int j = size - 1;
while (1) {
while (polar_angle(origin, points[i]) < pivot_angle) {
i++;
}
while (polar_angle(origin, points[j]) > pivot_angle) {
j--;
}

if (i >= j) {
break;
}

struct point tmp = points[i];
points[i] = points[j];
points[j] = tmp;

i++;
j--;
}

polar_angles_sort(points, origin, i);
polar_angles_sort(points + i, origin, size - i);
}

size_t graham_scan(struct point *points, size_t size) {
qsort(points, size, sizeof(struct point), cmp_points);
polar_angles_sort(points, points[0], size);

struct point tmp_points[size + 1];
memcpy(tmp_points + 1, points, size * sizeof(struct point));
tmp_points[0] = tmp_points[size];

size_t m = 1;
for (size_t i = 2; i <= size; ++i) {
while (ccw(tmp_points[m - 1], tmp_points[m], tmp_points[i]) <= 0) {
if (m > 1) {
m--;
continue;
} else if (i == size) {
break;
} else {
i++;
}
}

m++;
struct point tmp = tmp_points[i];
tmp_points[i] = tmp_points[m];
tmp_points[m] = tmp;
}

memcpy(points, tmp_points + 1, size * sizeof(struct point));

return m;
}

int main() {
struct point points[] = {{2.0, 1.9}, {1.0, 1.0}, {2.0, 4.0}, {3.0, 1.0},
{2.0, 0.0}};

printf("Points:\n");
for (size_t i = 0; i < 5; ++i) {
printf("(%f,%f)\n", points[i].x, points[i].y);
}

size_t hull_size = graham_scan(points, 5);

printf("\nHull:\n");
for (size_t i = 0; i < hull_size; ++i) {
printf("(%f,%f)\n", points[i].x, points[i].y);
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
{% method %}
{% sample lang="jl" %}
[import:30-32, lang:"julia"](code/julia/graham.jl)
{% sample lang="c" %}
[import:24-26, lang:"c_cpp"](code/c/graham.c)
{% endmethod %}

If the output of this function is 0, the points are collinear.
Expand All @@ -30,6 +32,8 @@ In the end, the code should look something like this:
{% method %}
{% sample lang="jl" %}
[import:34-69, lang:"julia"](code/julia/graham.jl)
{% sample lang="c" %}
[import:65-95, lang:"c_cpp"](code/c/graham.c)
{% endmethod %}

### Bibliography
Expand All @@ -41,6 +45,8 @@ In the end, the code should look something like this:
{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/graham.jl)
{% sample lang="c" %}
[import, lang:"c_cpp"](code/c/graham.c)
{% endmethod %}

<script>
Expand Down