Skip to content

Commit

Permalink
fix: ensure convex polygon does not panic due to invalid amount of po…
Browse files Browse the repository at this point in the history
…ints (#168)

* Ensure convex polygon has more than 2 vertices
* Ensure points passed to convex polygon aren't empty
  • Loading branch information
hakolao authored May 2, 2024
1 parent 75ade55 commit 1d530f8
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/shape/convex_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ 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<Point<Real>>) -> Option<Self> {
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.
for i1 in 0..points.len() {
let i2 = (i1 + 1) % points.len();
Expand Down Expand Up @@ -64,7 +66,7 @@ impl ConvexPolygon {
points.truncate(new_length);
normals.truncate(new_length);

if !points.is_empty() {
if points.len() > 2 {
Some(ConvexPolygon { points, normals })
} else {
None
Expand Down

0 comments on commit 1d530f8

Please sign in to comment.