fix polygons getting unclosed on mirroring
authoralfadur
Mon, 05 Nov 2018 20:22:09 +0300
changeset 14138 a65b60f36b96
parent 14137 95360f56db38
child 14139 09f62bb046ef
fix polygons getting unclosed on mirroring
rust/integral-geometry/src/lib.rs
rust/landgen/src/outline.rs
--- a/rust/integral-geometry/src/lib.rs	Mon Nov 05 19:56:28 2018 +0300
+++ b/rust/integral-geometry/src/lib.rs	Mon Nov 05 20:22:09 2018 +0300
@@ -464,11 +464,21 @@
         (&self.vertices[..self.edges_count()]).iter()
     }
 
-    pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut Point> + 'a {
+    fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut Point> + 'a {
         let edges_count = self.edges_count();
         (&mut self.vertices[..edges_count]).iter_mut()
     }
 
+    pub fn for_each<F>(&mut self, f: F)
+        where F: (Fn(&mut Point))
+    {
+        if !self.vertices.is_empty() {
+            self.iter_mut().for_each(f);
+            let edges_count = self.edges_count();
+            self.vertices[edges_count] = self.vertices[0]
+        }
+    }
+
     pub fn iter_edges<'a>(&'a self) -> impl Iterator<Item = Line> + 'a {
         (&self.vertices[0..self.edges_count()])
             .iter()
--- a/rust/landgen/src/outline.rs	Mon Nov 05 19:56:28 2018 +0300
+++ b/rust/landgen/src/outline.rs	Mon Nov 05 20:22:09 2018 +0300
@@ -54,11 +54,11 @@
             .chain(self.fill_points.iter())
     }
 
-    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
-        self.islands
-            .iter_mut()
-            .flat_map(|i| i.iter_mut())
-            .chain(self.fill_points.iter_mut())
+    pub fn for_each<F>(&mut self, f: F)
+        where F: (Fn(&mut Point))
+    {
+        self.islands.iter_mut().for_each(|p| p.for_each(|x| f(x)));
+        self.fill_points.iter_mut().for_each(|p| f(p));
     }
 
     fn divide_edge<I: Iterator<Item = u32>>(
@@ -289,14 +289,12 @@
 
     pub fn mirror(&mut self) {
         let r = self.size.width as i32 - 1;
-
-        self.iter_mut().for_each(|p| p.x = r - p.x);
+        self.for_each(|p| p.x = r - p.x);
     }
 
     pub fn flip(&mut self) {
         let t = self.size.height as i32 - 1;
-
-        self.iter_mut().for_each(|p| p.y = t - p.y);
+        self.for_each(|p| p.y = t - p.y);
     }
 }
 
@@ -323,7 +321,7 @@
         Some(&Line::new(Point::new(20, 15), Point::new(10, 15)))
     );
 
-    points.iter_mut().for_each(|p| p.x = 2);
+    points.for_each(|p| p.x = 2);
 
     assert_eq!(points.fill_points[0].x, 2);
     assert_eq!(points.islands[0].get_edge(0).start.x, 2);