Python Ramer-Douglas-Peucker RDP algorithm with number of points instead of epsilon
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
Classic Ramer-Douglas-Peucker simplifies a polyline by removing points whose distance from the retained segments stays below an epsilon tolerance. If you want a fixed number of output points instead of a distance threshold, you are solving a slightly different problem: control the simplification budget directly rather than the geometric error directly.
Why epsilon and Point Count Are Different
Standard RDP answers this question:
- “How much error am I willing to tolerate?”
A fixed-point variant answers a different one:
- “How many points may remain?”
Those are related, but not identical. Lower epsilon often keeps more points, yet there is no simple closed-form formula that maps a desired point count to the correct tolerance.
That is why point-count control needs either:
- repeated search over
epsilon - or a direct algorithm that keeps splitting until exactly
kpoints remain
A Practical Exact-Count Strategy
A good direct strategy is:
- start with the first and last points retained
- for every segment, find the interior point with the largest perpendicular error
- split the segment with the largest error
- repeat until you have the desired number of retained points
This behaves like “RDP with a priority queue” and gives direct control over the number of kept points.
Python Implementation
This keeps exactly k points as long as k is between 2 and the original number of points.
Binary Search on epsilon
Another approach is to run ordinary RDP repeatedly and binary-search the epsilon value until the result has roughly the desired number of points. That can work when “about k points” is acceptable.
But for exact point counts, binary search can be awkward because the number of retained points changes in discrete jumps. You may not hit the target exactly for every geometry.
That is why the direct priority-queue approach is often cleaner when the point budget is fixed.
Quality Tradeoff
The exact-count version gives strong control over output size, but it changes the primary optimization target. Standard RDP optimizes for distance tolerance; the fixed-count variant optimizes for “keep the most important splits until the point budget is exhausted.”
Those objectives are related, but not identical. So the output can differ from what you would get by choosing a particular epsilon.
When This Is Useful
A fixed number of points is useful when:
- a downstream model expects a fixed-length representation
- bandwidth or storage budget is strict
- you need the same output size for many shapes
In those cases, direct point-budget control is often more useful than geometric-tolerance control.
Common Pitfalls
- Treating “exactly
kpoints” as if it were the same problem as standard epsilon-based RDP. - Using binary search on
epsilonand expecting it to always land on the exact desired point count. - Forgetting that the first and last points are normally always retained, so the minimum practical count is usually
2. - Using the fixed-count result as if it guaranteed the same maximum error bound as standard epsilon-based simplification.
- Recomputing every segment naively without thinking about the cost when simplifying many large polylines.
Summary
- Standard RDP is controlled by
epsilon, not by a target point count. - If you need exactly
kpoints, a priority-queue split strategy is often the cleanest direct solution. - Binary search on
epsilonis useful for approximate count control, but not always for exact count control. - Fixed-count simplification changes the optimization objective from “error threshold” to “point budget.”
- Choose the method based on whether geometric tolerance or output size is the more important requirement.
Related reading
- Python recursive folder read
- Python string 'in' operator implementation algorithm and time complexity
- Python weighted median algorithm with pandas
- Pythonic way to check if a list is sorted or not
- Quadrilateral Shape Finding Algorithm
- QuadTree find neighbor
- Python, remove all non-alphabet chars from string
- python .replace regex

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.