Skip to content
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

Add Convex Hull in C #3958

Merged
merged 31 commits into from
Nov 16, 2024
Merged
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
efe02da
Add binary search in C
2Clutch Oct 27, 2024
d4c3e22
Merge branch 'TheRenegadeCoder:main' into master
2Clutch Oct 27, 2024
7251534
Add code
2Clutch Oct 27, 2024
ea9c9ff
Handle errors and potential edge cases
2Clutch Oct 27, 2024
b428138
Clean Up + Refactor + Request Input from Command Line
2Clutch Oct 27, 2024
bca6b20
Follow documentation to the letter
2Clutch Oct 27, 2024
6a1105c
for the win? (e.g. check that array is not empty)
2Clutch Oct 28, 2024
7e838d0
Clean Up & Refactor
2Clutch Oct 28, 2024
9f66116
Merge remote-tracking branch 'refs/remotes/origin/master'
2Clutch Oct 28, 2024
e6816cb
Clean Up & Refactor
2Clutch Oct 28, 2024
7028097
Fix error message
2Clutch Oct 28, 2024
de08b32
Update error message handling
2Clutch Oct 28, 2024
8b8d41b
Merge branch 'TheRenegadeCoder:main' into master
2Clutch Oct 28, 2024
29511a4
Add Convex Hull
2Clutch Oct 28, 2024
ee12a88
Merge branch 'TheRenegadeCoder:main' into master
2Clutch Oct 29, 2024
643eb70
Update test handling
2Clutch Oct 29, 2024
8c310de
Merge branch 'TheRenegadeCoder:main' into master
2Clutch Oct 29, 2024
d0f8213
Clean Up & Refactor
2Clutch Oct 29, 2024
51f982e
for the win?
2Clutch Oct 29, 2024
5dfa5c5
Merge branch 'TheRenegadeCoder:main' into master
2Clutch Oct 29, 2024
fddbd64
Merge remote-tracking branch 'refs/remotes/origin/master'
2Clutch Oct 29, 2024
fe4f138
Fix input validation and output format for convex hull calculation + …
2Clutch Oct 29, 2024
793dc0a
Add command line argument handling for x and y coordinates + Implemen…
2Clutch Oct 29, 2024
aa58d24
brand new implementation
2Clutch Oct 29, 2024
bd69cb4
Refactor error handling to eliminate code repetition for usage messages
2Clutch Oct 29, 2024
54d6812
Use same error message everywhere
2Clutch Oct 29, 2024
db2fd2e
Handle negative numbers
2Clutch Oct 29, 2024
6314765
Fix convex hull logic to correctly output hull points
2Clutch Oct 31, 2024
1afd977
Merge branch 'main' into master
rzuckerm Nov 3, 2024
b67e744
Merge branch 'main' into master
rzuckerm Nov 16, 2024
9f03c2b
Fix Convex Hull in C
rzuckerm Nov 16, 2024
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
134 changes: 134 additions & 0 deletions archive/c/c/convex-hull.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct {
int x;
int y;
} Point;

int compare(const void *p1, const void *p2) {
Point *point1 = (Point *)p1;
Point *point2 = (Point *)p2;

if (point1->x != point2->x) {
return point1->x - point2->x;
}
return point1->y - point2->y;
}

int orientation(Point p, Point q, Point r) {
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
return (val == 0) ? 0 : (val > 0) ? 1 : 2;
}

void convexHull(Point points[], int n) {
if (n < 3) return;

Point hull[n];

qsort(points, n, sizeof(Point), compare);

int l = 0;
for (int i = 1; i < n; i++)
if (points[i].x < points[l].x)
l = i;

int p = l, q;
do {
hull[0] = points[p];
q = (p + 1) % n;

for (int i = 0; i < n; i++) {
if (orientation(points[p], points[i], points[q]) == 2) {
q = i;
}
}

p = q;
} while (p != l);

for (int i = 0; i < n; i++) {
bool found = false;
for (int j = 0; j < n; j++) {
if (hull[j].x == points[i].x && hull[j].y == points[i].y) {
found = true;
break;
}
}
if (!found) {
hull[n++] = points[i];
}
}

for (int i = 0; i < n; i++) {
printf("(%d, %d)\n", hull[i].x, hull[i].y);
}
}

bool isInteger(const char *s) {
while (*s) {
if (*s < '0' || *s > '9') {
return false;
}
s++;
}
return true;
}

void parseCoordinates(char *inputX, char *inputY) {
char *tokenX = strtok(inputX, ",");
char *tokenY = strtok(inputY, ",");

Point *points = NULL;
int count = 0;

while (tokenX && tokenY) {
if (!isInteger(tokenX) || !isInteger(tokenY)) {
printf("Invalid Integers\n");
return;
}

points = realloc(points, sizeof(Point) * (count + 1));
points[count].x = atoi(tokenX);
points[count].y = atoi(tokenY);
count++;

tokenX = strtok(NULL, ",");
tokenY = strtok(NULL, ",");
}

if (tokenX || tokenY) {
printf("Different Cardinality\n");
free(points);
return;
}

if (count < 3) {
printf("Usage: please provide at least 3 x and y coordinates as separate lists (e.g. \"100, 440, 210\")\n");
free(points);
return;
}

convexHull(points, count);
free(points);
}

int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: please provide two coordinate lists (x, y)\n");
return 1;
}

char *inputX = argv[1];
char *inputY = argv[2];

if (strlen(inputY) == 0) {
printf("Missing Y\n");
return 1;
}

parseCoordinates(inputX, inputY);
return 0;
}
Loading