Convex hull: Difference between revisions
C code replaced by more accurate translation
m (→plot: c) |
(C code replaced by more accurate translation) |
||
Line 107:
=={{header|C}}==
{{trans|C++}}
<lang C>#include <
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct tPoint {
int x, y;
} Point;
bool ccw(const Point *a, const Point *b, const Point *c) {
return (b->x - a->x) * (c->y - a->y)
> (b->y - a->y) * (c->x - a->x);
}
int
const Point* lp =
const Point* rp =
if (lp
if (rp->x < lp->x)
return 1;
if (lp->y < rp->y)
return -1;
if (rp->y < lp->y)
return 1;
return 0;
}
void
}
void* xmalloc(size_t n) {
if (ptr == NULL)
return ptr;
}
void* xrealloc(void* p, size_t n) {
if (ptr == NULL)
return ptr;
}
void
printf("[");
if (
printf("(%d, %d)", ptr->x, ptr->y);
}
printf("]");
}
Point* convexHull(Point p[], int len, int* hsize) {
if (len == 0) {
*hsize = 0;
return NULL;
}
int i, size = 0, capacity = 4;
Point* hull = xmalloc(capacity * sizeof(Point));
qsort(p, len, sizeof(Point), comparePoints);
/* lower hull */
for (i = 0; i < len; ++i) {
while (
capacity
hull = xrealloc(hull, capacity * sizeof(Point));
}
assert(size >= 0 && size < capacity);
}
/* upper hull */
int t = size + 1;
for (i = len - 1; i >= 0; i--) {
while (
capacity
hull = xrealloc(hull, capacity * sizeof(Point));
}
assert(size >= 0 && size < capacity);
}
--size;
hull = xrealloc(hull, size * sizeof(Point));
*hsize = size;
return hull;
}
int main() {
Point points[] = {
Line 253 ⟶ 216:
{-3, -9}, { 0, 11}, {-9, -3}, {-4, -2}, {12, 10}
};
int hsize;
printf("
printf("\n");
free(hull);
return 0;
}</lang>
|