Skip to main content

simplify_polygon_ring

Function simplify_polygon_ring 

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

Simplify a closed polygon ring using Douglas-Peucker.

Like simplify_douglas_peucker but guarantees the result remains a valid ring:

  • The first and last vertices are always retained.
  • If the ring has a closing duplicate (first == last), it is preserved in the output so the ring stays closed.
  • The result is guaranteed to have at least 3 distinct vertices (plus the optional closing duplicate), because a polygon with fewer than 3 vertices is degenerate. If simplification would reduce the ring below this minimum, the original ring is returned unchanged.

ยงExample

use rustial_engine::simplify_polygon_ring;
use rustial_engine::GeoCoord;

let ring = vec![
    GeoCoord::from_lat_lon(0.0, 0.0),
    GeoCoord::from_lat_lon(0.0, 1.0),
    GeoCoord::from_lat_lon(1.0, 1.0),
    GeoCoord::from_lat_lon(1.0, 0.0),
    GeoCoord::from_lat_lon(0.0, 0.0), // closing vertex
];
let simplified = simplify_polygon_ring(&ring, 0.01);
// All corners are significant; result keeps them.
assert!(simplified.len() >= 4); // 4 corners or 4 + closing