How to subsample a 2D polygon?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Subsampling a polygon can mean two different things: keeping fewer existing vertices, or generating a new set of points that samples the boundary more evenly. The right method depends on whether you care more about preserving exact corners or producing a smoother, lower-resolution representation.
Decide What “Subsample” Means First
If your polygon is a noisy contour with far too many points, you usually want simplification. In that case, the goal is to remove redundant vertices while keeping the overall shape.
If your polygon comes from irregular measurements and you want a fixed number of evenly distributed boundary points, you want resampling instead. That creates new points along edges, not just a filtered subset of the old list.
Those are different operations, and confusing them leads to poor results.
Why Taking Every Nth Vertex Is Usually Wrong
The simplest idea is to keep every kth point. That is easy to implement, but it assumes the original vertices are already evenly spaced. Real polygon data often violates that assumption.
Imagine one long straight edge represented by fifty points and one sharp corner represented by two points. Taking every fifth vertex may keep too many points on the straight edge and accidentally drop the important corner.
That is why practical polygon subsampling usually follows one of two strategies:
- distance or tolerance based simplification for shape preservation
- arc-length resampling for evenly spaced output points
Evenly Resample Along the Perimeter
The Python code below walks around a closed polygon and places points at equal distances along the boundary. It is runnable and uses only the standard library.
This is a good choice when downstream code expects a fixed number of boundary samples, such as feature extraction or machine-learning preprocessing.
Simplify When You Need Fewer Meaningful Vertices
If the goal is to preserve corners and remove only unnecessary points, use a simplification algorithm such as Ramer-Douglas-Peucker. That algorithm keeps points whose perpendicular distance from a simplified edge exceeds a tolerance and removes the rest.
Conceptually, it works like this:
- connect the start and end of a chain
- find the point farthest from that line
- keep it only if the deviation is large enough
- recurse on the remaining pieces
For polygons, you usually apply that process to the closed boundary with care around the seam where the polygon wraps back to the first point.
Choosing a Tolerance or Sample Count
There is no universal best value. A small tolerance keeps more detail; a large tolerance produces fewer points but can visibly distort the outline. Likewise, a low sample count in perimeter resampling can round off sharp features simply because you did not allocate enough output points.
The parameter should match the scale of the geometry. A tolerance of 0.5 means very different things for coordinates measured in pixels, meters, or latitude-longitude degrees.
Common Pitfalls
The most common mistake is forgetting that polygons are closed. If your algorithm treats the data as an open polyline, the first and last edge may be ignored, which changes the shape.
Another mistake is mixing simplification and resampling terminology. If you need evenly spaced samples, a simplifier is the wrong tool. If you need to preserve important corners, uniform resampling may hide them.
Degenerate input is another problem. Repeated consecutive points and zero-length edges can break distance calculations or produce division-by-zero errors. Clean the polygon before subsampling.
Summary
- Polygon subsampling can mean simplification or even boundary resampling.
- Taking every
kth vertex is only safe when the original points are already evenly spaced. - Use perimeter-based resampling when you need a fixed number of evenly distributed points.
- Use tolerance-based simplification when you want fewer vertices while preserving shape.
- Always handle polygon closure and remove degenerate edges before processing.
Related reading
- How to tell if an array is a permutation in On?
- How to tell if greedy algorithm suffices for finding minimum coin change?
- How to test a hash function?
- How to test if one string is a subsequence of another?
- How to test if a kernel is a valid kernel
- How to turn integers into Fibonacci coding efficiently?
- How to think in recursive way?
- How to trace the path in a Breadth-First Search?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.