From e3f5f944ed90fa7fb96ad2b670ea34c0765df1ad Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 24 Nov 2020 11:41:33 +0100 Subject: Fix polygon corners not always running counterclockwise --- src/math/polygon/polygon_graph.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/math/polygon/polygon_graph.rs') diff --git a/src/math/polygon/polygon_graph.rs b/src/math/polygon/polygon_graph.rs index 9477fbc..6fdb6cd 100644 --- a/src/math/polygon/polygon_graph.rs +++ b/src/math/polygon/polygon_graph.rs @@ -261,7 +261,7 @@ impl PolygonGraph { let mut current_node = &self.nodes[0]; // Pretend we're coming from the top to start in the right direction. let mut last_vec = current_node.vec - Vec2::new(T::zero(), T::one()); - let mut bounding_polygon = Polygon::new(vec![current_node.vec]); + let mut bounding_corners = vec![current_node.vec]; loop { /* Find the next point by choosing the one with the greatest angle. This means we are * "bending" to the leftmost edge at each step. Since we are going around the polygon @@ -281,17 +281,17 @@ impl PolygonGraph { .expect("Adjacency list is empty. The polygon has an open edge (is broken)"); // When we have come back to the start, the traversal is completed - if *next_corner == bounding_polygon.corners[0] { + if *next_corner == bounding_corners[0] { break; } - bounding_polygon.corners.push(*next_corner); + bounding_corners.push(*next_corner); last_vec = current_node.vec; current_node = &self.nodes[find_node(&self.nodes, &next_corner) .expect("Failure to find node that should be inside list.")]; } - bounding_polygon + Polygon::new(bounding_corners) } } @@ -433,15 +433,15 @@ mod test { assert_eq!( bounding.corners[(start_i + 1) % num_corners], - Vec2::new(2., 0.) + Vec2::new(0., 2.) ); assert_eq!( bounding.corners[(start_i + 2) % num_corners], - Vec2::new(2.5, -0.5) + Vec2::new(2., 2.) ); assert_eq!( bounding.corners[(start_i + 3) % num_corners], - Vec2::new(2.5, 0.0) + Vec2::new(3., 1.) ); assert_eq!( bounding.corners[(start_i + 4) % num_corners], @@ -449,15 +449,15 @@ mod test { ); assert_eq!( bounding.corners[(start_i + 5) % num_corners], - Vec2::new(3., 1.) + Vec2::new(2.5, 0.0) ); assert_eq!( bounding.corners[(start_i + 6) % num_corners], - Vec2::new(2., 2.) + Vec2::new(2.5, -0.5) ); assert_eq!( bounding.corners[(start_i + 7) % num_corners], - Vec2::new(0., 2.) + Vec2::new(2., 0.) ); } } -- cgit v1.2.3-70-g09d2