-
-
Notifications
You must be signed in to change notification settings - Fork 632
Add Convex Hull in C #3958
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
Add Convex Hull in C #3958
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 d4c3e22
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 7251534
Add code
2Clutch ea9c9ff
Handle errors and potential edge cases
2Clutch b428138
Clean Up + Refactor + Request Input from Command Line
2Clutch bca6b20
Follow documentation to the letter
2Clutch 6a1105c
for the win? (e.g. check that array is not empty)
2Clutch 7e838d0
Clean Up & Refactor
2Clutch 9f66116
Merge remote-tracking branch 'refs/remotes/origin/master'
2Clutch e6816cb
Clean Up & Refactor
2Clutch 7028097
Fix error message
2Clutch de08b32
Update error message handling
2Clutch 8b8d41b
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 29511a4
Add Convex Hull
2Clutch ee12a88
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 643eb70
Update test handling
2Clutch 8c310de
Merge branch 'TheRenegadeCoder:main' into master
2Clutch d0f8213
Clean Up & Refactor
2Clutch 51f982e
for the win?
2Clutch 5dfa5c5
Merge branch 'TheRenegadeCoder:main' into master
2Clutch fddbd64
Merge remote-tracking branch 'refs/remotes/origin/master'
2Clutch fe4f138
Fix input validation and output format for convex hull calculation + …
2Clutch 793dc0a
Add command line argument handling for x and y coordinates + Implemen…
2Clutch aa58d24
brand new implementation
2Clutch bd69cb4
Refactor error handling to eliminate code repetition for usage messages
2Clutch 54d6812
Use same error message everywhere
2Clutch db2fd2e
Handle negative numbers
2Clutch 6314765
Fix convex hull logic to correctly output hull points
2Clutch 1afd977
Merge branch 'main' into master
rzuckerm b67e744
Merge branch 'main' into master
rzuckerm 9f03c2b
Fix Convex Hull in C
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
2Clutch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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"); | ||
2Clutch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return 1; | ||
| } | ||
|
|
||
| char *inputX = argv[1]; | ||
| char *inputY = argv[2]; | ||
|
|
||
| if (strlen(inputY) == 0) { | ||
| printf("Missing Y\n"); | ||
2Clutch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return 1; | ||
| } | ||
|
|
||
| parseCoordinates(inputX, inputY); | ||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.