Polygon Subsampling
2D Geometry
Computational Geometry
Data Reduction
Spatial Algorithms

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.

Practice algorithms

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.

python
1from math import hypot
2
3
4def resample_polygon(points, samples):
5    if len(points) < 3:
6        raise ValueError("a polygon needs at least three points")
7    if samples < 3:
8        raise ValueError("need at least three output samples")
9
10    closed = points + [points[0]]
11    lengths = []
12    perimeter = 0.0
13
14    for a, b in zip(closed, closed[1:]):
15        segment = hypot(b[0] - a[0], b[1] - a[1])
16        lengths.append(segment)
17        perimeter += segment
18
19    step = perimeter / samples
20    result = []
21    edge_index = 0
22    edge_start = closed[0]
23    edge_end = closed[1]
24    edge_offset = 0.0
25
26    for i in range(samples):
27        target = i * step
28        while edge_offset + lengths[edge_index] < target:
29            edge_offset += lengths[edge_index]
30            edge_index += 1
31            edge_start = closed[edge_index]
32            edge_end = closed[edge_index + 1]
33
34        distance_on_edge = target - edge_offset
35        ratio = distance_on_edge / lengths[edge_index] if lengths[edge_index] else 0.0
36        x = edge_start[0] + ratio * (edge_end[0] - edge_start[0])
37        y = edge_start[1] + ratio * (edge_end[1] - edge_start[1])
38        result.append((round(x, 3), round(y, 3)))
39
40    return result
41
42
43square = [(0, 0), (4, 0), (4, 4), (0, 4)]
44print(resample_polygon(square, 8))

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:

  1. connect the start and end of a chain
  2. find the point farthest from that line
  3. keep it only if the deviation is large enough
  4. 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.