Skip to main content

simplify_douglas_peucker

Function simplify_douglas_peucker 

Source
pub fn simplify_douglas_peucker(
    coords: &[GeoCoord],
    epsilon: f64,
) -> Vec<GeoCoord>
Expand description

Simplify a polyline using the Douglas-Peucker algorithm.

epsilon is the maximum allowed perpendicular distance in degrees (see module docs for the coordinate-space rationale). Smaller values retain more detail; zero retains every vertex.

§Returns

A new Vec<GeoCoord> containing only the vertices that exceed the tolerance threshold, plus the first and last vertices which are always retained.

§Edge cases

InputResult
empty sliceempty Vec
1 vertexthat vertex (copied)
2 verticesboth vertices (copied)
epsilon <= 0.0all vertices (no simplification)
epsilon is NaNall vertices (no simplification – NaN comparisons always false)

§Example

use rustial_engine::simplify_douglas_peucker;
use rustial_engine::GeoCoord;

let line = vec![
    GeoCoord::from_lat_lon(0.0, 0.0),
    GeoCoord::from_lat_lon(0.0, 0.5),  // collinear -- will be removed
    GeoCoord::from_lat_lon(0.0, 1.0),
];
let simplified = simplify_douglas_peucker(&line, 0.01);
assert_eq!(simplified.len(), 2);