-
-
Notifications
You must be signed in to change notification settings - Fork 50.8k
feat: add Ramer-Douglas-Peucker polyline simplification algorithm #14372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
poyea
merged 10 commits into
TheAlgorithms:master
from
AliAlimohammadi:ramer-douglas-peucker
May 19, 2026
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
10f1823
feat: add Ramer-Douglas-Peucker polyline simplification algorithm
AliAlimohammadi 7bfabc3
Use descriptive parameter names
AliAlimohammadi f305c16
Merge branch 'master' into ramer-douglas-peucker
AliAlimohammadi a173da4
Merge branch 'master' into ramer-douglas-peucker
AliAlimohammadi 56b681e
Update geometry/ramer_douglas_peucker.py
poyea 3f5c2b4
Update geometry/ramer_douglas_peucker.py
poyea 007e72c
Merge branch 'master' into ramer-douglas-peucker
poyea d403421
Update ramer_douglas_peucker.py
AliAlimohammadi b8d87e6
Update ramer_douglas_peucker.py
AliAlimohammadi f3e1a56
Update ramer_douglas_peucker.py
AliAlimohammadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| """ | ||
| Ramer-Douglas-Peucker (RDP) algorithm for polyline simplification. | ||
|
|
||
| Given a curve represented as a sequence of points and a tolerance epsilon, | ||
| the algorithm recursively reduces the number of points while preserving the | ||
| overall shape of the curve. Points that deviate from the simplified line by | ||
| less than epsilon are removed. | ||
|
|
||
| Time complexity: O(n log n) on average, O(n²) worst case | ||
| Space complexity: O(n) for the recursion stack | ||
|
|
||
| References: | ||
| - https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm | ||
| - Ramer, U. (1972). "An iterative procedure for the polygonal approximation | ||
| of plane curves". Computer Graphics and Image Processing. 1 (3): 244-256. | ||
| - Douglas, D.; Peucker, T. (1973). "Algorithms for the reduction of the number | ||
| of points required to represent a digitized line or its caricature". | ||
| Cartographica. 10 (2): 112-122. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math | ||
| from collections.abc import Sequence | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Internal helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def _euclidean_distance( | ||
| point_1: tuple[float, float], point_2: tuple[float, float] | ||
| ) -> float: | ||
| """Return the Euclidean distance between two 2-D points. | ||
|
|
||
| >>> _euclidean_distance((0.0, 0.0), (3.0, 4.0)) | ||
| 5.0 | ||
| >>> _euclidean_distance((1.0, 1.0), (1.0, 1.0)) | ||
| 0.0 | ||
| """ | ||
| return math.hypot(point_2[0] - point_1[0], point_2[1] - point_1[1]) | ||
|
|
||
|
|
||
| def _perpendicular_distance( | ||
| point: tuple[float, float], | ||
| line_start: tuple[float, float], | ||
| line_end: tuple[float, float], | ||
| ) -> float: | ||
| """Return the perpendicular distance from *point* to the line through | ||
| *line_start* and *line_end*. | ||
|
|
||
| The result is the absolute value of the signed area of the triangle | ||
| (line_start, line_end, point) divided by the length of the segment, which | ||
| equals the altitude of that triangle from point. | ||
|
|
||
| >>> _perpendicular_distance((4.0, 0.0), (0.0, 0.0), (0.0, 3.0)) | ||
| 4.0 | ||
| >>> # order of line_start and line_end does not affect the result | ||
| >>> _perpendicular_distance((4.0, 0.0), (0.0, 3.0), (0.0, 0.0)) | ||
| 4.0 | ||
| >>> _perpendicular_distance((4.0, 1.0), (0.0, 1.0), (0.0, 4.0)) | ||
| 4.0 | ||
| >>> _perpendicular_distance((2.0, 1.0), (-2.0, 1.0), (-2.0, 4.0)) | ||
| 4.0 | ||
| """ | ||
| px, py = point | ||
| ax, ay = line_start | ||
| bx, by = line_end | ||
| numerator = abs((by - ay) * px - (bx - ax) * py + bx * ay - by * ax) | ||
| denominator = _euclidean_distance(line_start, line_end) | ||
| if denominator == 0.0: | ||
| # line_start and line_end coincide; fall back to point-to-point distance | ||
| return _euclidean_distance(point, line_start) | ||
| return numerator / denominator | ||
|
|
||
|
|
||
|
AliAlimohammadi marked this conversation as resolved.
Outdated
|
||
| # --------------------------------------------------------------------------- | ||
| # Public API | ||
| # --------------------------------------------------------------------------- | ||
|
poyea marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def ramer_douglas_peucker( | ||
| points: Sequence[tuple[float, float]], | ||
| epsilon: float, | ||
| ) -> list[tuple[float, float]]: | ||
| """Simplify a polyline using the Ramer-Douglas-Peucker algorithm. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| points: | ||
| An ordered sequence of ``(x, y)`` tuples that form the polyline. | ||
| epsilon: | ||
| Maximum allowable perpendicular deviation. Points whose distance | ||
| to the simplified segment is less than or equal to *epsilon* are | ||
| discarded. Must be non-negative. | ||
|
|
||
| Returns | ||
| ------- | ||
| list[tuple[float, float]] | ||
| A simplified list of ``(x, y)`` points that is a subset of *points*. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If *epsilon* is negative. | ||
|
|
||
| Examples | ||
| -------- | ||
| Collinear points - middle point is redundant for any positive epsilon: | ||
|
|
||
| >>> ramer_douglas_peucker([(0.0, 0.0), (1.0, 0.0), (2.0, 0.0)], epsilon=0.5) | ||
| [(0.0, 0.0), (2.0, 0.0)] | ||
|
|
||
| Empty / tiny inputs are returned unchanged: | ||
|
|
||
| >>> ramer_douglas_peucker([], epsilon=1.0) | ||
| [] | ||
| >>> ramer_douglas_peucker([(0.0, 0.0)], epsilon=1.0) | ||
| [(0.0, 0.0)] | ||
| >>> ramer_douglas_peucker([(0.0, 0.0), (1.0, 1.0)], epsilon=1.0) | ||
| [(0.0, 0.0), (1.0, 1.0)] | ||
|
|
||
| A simple square outline where epsilon removes mid-edge points: | ||
|
|
||
| >>> square = [ | ||
| ... (0.0, 0.0), (1.0, 0.0), (2.0, 0.0), | ||
| ... (2.0, 1.0), (2.0, 2.0), (1.0, 2.0), | ||
| ... (0.0, 2.0), (0.0, 1.0), | ||
| ... ] | ||
| >>> ramer_douglas_peucker(square, epsilon=0.7) | ||
| [(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 1.0)] | ||
|
|
||
| A polygonal chain simplified to a single segment for large epsilon: | ||
|
|
||
| >>> chain = [(0.0, 0.0), (2.0, 0.5), (3.0, 3.0), (6.0, 3.0), (8.0, 4.0)] | ||
| >>> ramer_douglas_peucker(chain, epsilon=3.0) | ||
| [(0.0, 0.0), (8.0, 4.0)] | ||
|
|
||
| Zero epsilon keeps all points: | ||
|
|
||
| >>> ramer_douglas_peucker([(0.0, 0.0), (0.5, 0.1), (1.0, 0.0)], epsilon=0.0) | ||
| [(0.0, 0.0), (0.5, 0.1), (1.0, 0.0)] | ||
| """ | ||
| if epsilon < 0: | ||
| msg = f"epsilon must be non-negative, got {epsilon!r}" | ||
| raise ValueError(msg) | ||
|
AliAlimohammadi marked this conversation as resolved.
|
||
|
|
||
| pts = list(points) | ||
|
|
||
| if len(pts) < 3: | ||
| return pts | ||
|
|
||
| # Find the point with the greatest perpendicular distance from the line | ||
| # connecting the first and last points. | ||
| start, end = pts[0], pts[-1] | ||
| max_dist = 0.0 | ||
| max_index = 0 | ||
| for i in range(1, len(pts) - 1): | ||
| dist = _perpendicular_distance(pts[i], start, end) | ||
| if dist > max_dist: | ||
| max_dist = dist | ||
| max_index = i | ||
|
|
||
| if max_dist > epsilon: | ||
| # Recursively simplify both halves and join them (drop the duplicate | ||
| # point at the junction). | ||
| left = ramer_douglas_peucker(pts[: max_index + 1], epsilon) | ||
| right = ramer_douglas_peucker(pts[max_index:], epsilon) | ||
| return left[:-1] + right | ||
|
AliAlimohammadi marked this conversation as resolved.
Outdated
|
||
|
|
||
| # All intermediate points are within tolerance - keep only the endpoints. | ||
| return [start, end] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.