Geometry processing playground focused on convex hull construction, implemented in Python and visualized with matplotlib
.
convex_hull.py computes the convex hull of a set of 2D points.
- Find the starting point by selecting the lowest point (or the leftmost if tied).
- Sort all points by polar angle relative to the starting point to determine processing order.
- Build the hull contour by iterating through the sorted points, removing any that introduce a concave (clockwise) turn before adding the next point.
The result is the smallest convex polygon enclosing all given points:
convex_hull_animated.py visualizes the step-by-step construction of a convex hull using Graham's scan. Each animation frame highlights a point as it is evaluated for inclusion: it is added if it maintains a counterclockwise turn or removed if it introduces a concave (clockwise) turn.
- Sort points by x-coordinate (and y if tied) to establish a processing order.
- Build the lower hull by iterating from left to right, adding points and removing concave turns.
- Build the upper hull by iterating in reverse, applying the same counterclockwise rule.
- Animate each step, highlighting points as they are evaluated, added, or removed.
- Display the complete hull contour as the final animation frame.
The dashed red contour dynamically updates as the hull forms, and once all points are processed, the final convex hull is displayed as a solid red polygon:
defect_detection.py identifies convexity defects, i.e., concave points where a shape deviates inward from its convex hull. These defects are detected by identifying concave points and visualizing their shortest distance to the hull.
Steps:
- Compute the convex hull of a given set of 2D points.
- Identify concave points (not part of the convex hull).
- Determine the shortest distance from each concave point to its nearest hull edge.
- Visualize defects by marking concave points and drawing dotted lines to their nearest hull edge.
The visualization highlights hull vertices (orange), concave defect points (purple), and dotted projection lines (blue) indicating the depth of each defect: