From 21ce5c5bea6a743ce43ef3a418b108321be4b600 Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Fri, 13 Oct 2023 10:47:11 +0300 Subject: [PATCH 1/3] Ensure convex polygon has more than 2 vertices --- src/shape/convex_polygon.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/shape/convex_polygon.rs b/src/shape/convex_polygon.rs index 81205823..502368d3 100644 --- a/src/shape/convex_polygon.rs +++ b/src/shape/convex_polygon.rs @@ -34,7 +34,6 @@ impl ConvexPolygon { pub fn from_convex_polyline(mut points: Vec>) -> Option { let eps = ComplexField::sqrt(crate::math::DEFAULT_EPSILON); let mut normals = Vec::with_capacity(points.len()); - // First, compute all normals. for i1 in 0..points.len() { let i2 = (i1 + 1) % points.len(); @@ -63,8 +62,8 @@ impl ConvexPolygon { let new_length = points.len() - nremoved; points.truncate(new_length); normals.truncate(new_length); - - if !points.is_empty() { + + if points.len() > 2 { Some(ConvexPolygon { points, normals }) } else { None From 10ef9886f0ce1e644fc659c9a6bf4b52d71f03ce Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Fri, 13 Oct 2023 10:52:41 +0300 Subject: [PATCH 2/3] Ensure points passed to convex polygon aren't empty --- src/shape/convex_polygon.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/shape/convex_polygon.rs b/src/shape/convex_polygon.rs index 502368d3..7f092173 100644 --- a/src/shape/convex_polygon.rs +++ b/src/shape/convex_polygon.rs @@ -32,6 +32,9 @@ impl ConvexPolygon { /// Convexity of the input polyline is not checked. /// Returns `None` if all points form an almost flat line. pub fn from_convex_polyline(mut points: Vec>) -> Option { + if points.is_empty() { + return None; + } let eps = ComplexField::sqrt(crate::math::DEFAULT_EPSILON); let mut normals = Vec::with_capacity(points.len()); // First, compute all normals. From e5b0d9df9e1f305bc1e14c2c177affa92f721ed1 Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Wed, 7 Feb 2024 12:00:15 +0200 Subject: [PATCH 3/3] Fmt --- src/shape/convex_polygon.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shape/convex_polygon.rs b/src/shape/convex_polygon.rs index 7f092173..8a812053 100644 --- a/src/shape/convex_polygon.rs +++ b/src/shape/convex_polygon.rs @@ -65,8 +65,8 @@ impl ConvexPolygon { let new_length = points.len() - nremoved; points.truncate(new_length); normals.truncate(new_length); - - if points.len() > 2 { + + if points.len() > 2 { Some(ConvexPolygon { points, normals }) } else { None