Building a convex hull can be done using various algorithms, and there are ways to incrementally update the convex hull as new points are added. A common iterative approach involves maintaining the hull and checking new points to see if they affect the current convex hull.
Here, I'll provide a simple implementation using the Andrew's monotone chain algorithm, which is typically efficient but requires sorting. However, as you requested an iterative approach without sorting all points each time, we'll maintain a list of points and rebuild the convex hull whenever a new point is added, rather than sorting all points.
C Implementation
#include <stdio.h>
#include <stdlib.h>
typedef struct {
double x;
double y;
} Point;
// Function to calculate the cross product of vectors OA and OB
// A positive cross product indicates a counter-clockwise turn, and a negative indicates a clockwise turn
double cross(Point O, Point A, Point B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
// Function to compute the convex hull
void computeConvexHull(Point* points, int numPoints, Point** hull, int* hullSize) {
int k = 0;
// Lower hull
Point* lower = (Point*)malloc(numPoints * sizeof(Point));
for (int i = 0; i < numPoints; ++i) {
while (k >= 2 && cross(lower[k - 2], lower[k - 1], points[i]) <= 0) {
k--; // Remove the last point from lower hull
}
lower[k++] = points[i];
}
// Upper hull
k = 0;
for (int i = numPoints - 1; i >= 0; --i) {
while (k >= 2 && cross(lower[k - 2], lower[k - 1], points[i]) <= 0) {
k--; // Remove the last point from upper hull
}
lower[k++] = points[i];
}
// Remove the last point of each half because it is repeated at the beginning of the other half
*hullSize = k - 1; // adjust for the repeated point
*hull = (Point*)malloc((*hullSize) * sizeof(Point));
for (int i = 0; i < *hullSize; i++) {
(*hull)[i] = lower[i];
}
free(lower);
}
// Add a new point and re-compute the convex hull
void addPointAndRebuild(Point point, Point** hull, int* hullSize, Point** existingPoints, int* numExistingPoints) {
// Add the new point to the array of existing points
existingPoints[*numExistingPoints] = (Point*)malloc(sizeof(Point));
*(existingPoints[*numExistingPoints]) = point;
(*numExistingPoints)++;
// Rebuild the convex hull
computeConvexHull(*existingPoints, *numExistingPoints, hull, hullSize);
}
// Utility function to print points
void printPoints(Point* points, int size) {
for (int i = 0; i < size; i++) {
printf("(%lf, %lf)\n", points[i].x, points[i].y);
}
}
// Main function for demonstration
int main() {
int numPoints = 0;
int hullSize = 0;
Point* hull = NULL;
Point** existingPoints = (Point**)malloc(0); // Initialize existing points array
int existingPointsSize = 0;
// Add points iteratively
addPointAndRebuild((Point){0, 0}, &hull, &hullSize, existingPoints, &existingPointsSize);
addPointAndRebuild((Point){2, 1}, &hull, &hullSize, existingPoints, &existingPointsSize);
addPointAndRebuild((Point){1, 2}, &hull, &hullSize, existingPoints, &existingPointsSize);
addPointAndRebuild((Point){2, 2}, &hull, &hullSize, existingPoints, &existingPointsSize);
addPointAndRebuild((Point){1, 1}, &hull, &hullSize, existingPoints, &existingPointsSize);
addPointAndRebuild((Point){0, 2}, &hull, &hullSize, existingPoints, &existingPointsSize);
printf("Convex Hull:\n");
printPoints(hull, hullSize);
// Cleanup
free(hull);
for (int i = 0; i < existingPointsSize; i++) {
free(existingPoints[i]);
}
free(existingPoints);
return 0;
}
Explanation of the Code
-
Data Structures:
Point: A simple structure to represent a 2D point with x and y coordinates.
- The function pointer to the convex hull and array sizes are maintained throughout the program.
-
Cross Product Function:
- Computes the cross product of vectors to determine the orientation relative to the hull.
-
Compute Convex Hull Function:
- Takes existing points and computes the lower and upper hull using the cross product to ensure they turn counter-clockwise.
- Merges these into the final convex hull.
-
Add Point and Rebuild:
- On adding a new point, it dynamically increases the existing points array and calls the compute function to rebuild the hull.
-
Main Function:
- Demonstrates the usage of the functions above by adding several points and printing out the resulting convex hull.
Caveats and Modifications
- Memory Management: In production code, ensure robust memory management to avoid memory leaks.
- Performance: The rebuilding step is performed from scratch, which may not be efficient for large datasets. However, this fulfills your requirement of not sorting the entire list.
- Dynamic Array Management: In a more advanced implementation, you might consider using dynamic arrays or linked lists for more flexible adding of points.
Feel free to adapt the code further based on your specific requirements and constraints!