diff --git a/geom/src/polygon.rs b/geom/src/polygon.rs index 5c7ca2707b..682e7ecd67 100644 --- a/geom/src/polygon.rs +++ b/geom/src/polygon.rs @@ -146,7 +146,7 @@ impl Polygon { /// Top-left at the origin. Doesn't take Distance, because this is usually pixels, actually. pub fn maybe_rectangle(width: f64, height: f64) -> Result { - Ring::new(vec![ + Ring::strict_new(vec![ Pt2D::new(0.0, 0.0), Pt2D::new(width, 0.0), Pt2D::new(width, height), diff --git a/geom/src/ring.rs b/geom/src/ring.rs index e7abe61b98..5a39b09a92 100644 --- a/geom/src/ring.rs +++ b/geom/src/ring.rs @@ -43,6 +43,14 @@ impl Ring { Ok(result) } + pub fn strict_new(pts: Vec) -> Result { + // Enforce no duplicate adjacent points + if let Some(pair) = pts.windows(2).find(|pair| pair[0] == pair[1]) { + bail!("Ring has duplicate adjacent points near {}", pair[0]); + } + Self::new(pts) + } + pub fn must_new(pts: Vec) -> Ring { Ring::new(pts).unwrap() }