Don't generate unnecessary duplication in case of equal coordinates
authorunc0rr
Thu, 18 Oct 2018 22:34:09 +0200
changeset 13948 a325ed57ebfe
parent 13947 7e7a03e85ac4
child 13949 4162ea9ae333
Don't generate unnecessary duplication in case of equal coordinates
rust/integral-geometry/src/lib.rs
rust/land2d/src/lib.rs
--- a/rust/integral-geometry/src/lib.rs	Thu Oct 18 22:23:25 2018 +0200
+++ b/rust/integral-geometry/src/lib.rs	Thu Oct 18 22:34:09 2018 +0200
@@ -170,7 +170,7 @@
     pub fn new(vector: Point) -> Self {
         Self {
             vector,
-            iteration: 0,
+            iteration: if vector.x == vector.y { 4 } else { 8 },
         }
     }
 }
@@ -179,7 +179,7 @@
     type Item = Point;
 
     fn next(&mut self) -> Option<Self::Item> {
-        if self.iteration < 8 {
+        if self.iteration > 0 {
             self.vector.x = -self.vector.x;
             if self.iteration & 1 == 0 {
                 self.vector.y = -self.vector.y;
@@ -189,7 +189,7 @@
                 std::mem::swap(&mut self.vector.x, &mut self.vector.y);
             }
 
-            self.iteration += 1;
+            self.iteration -= 1;
 
             Some(self.vector)
         } else {
@@ -236,7 +236,7 @@
     }
 
     #[test]
-    fn equidistant() {
+    fn equidistant_full() {
         let n = EquidistantPoints::new(Point::new(1, 3));
         let v = get_points(&[
             (-1, -3),
@@ -254,4 +254,14 @@
             assert_eq!(a, b);
         }
     }
+
+    #[test]
+    fn equidistant_half() {
+        let n = EquidistantPoints::new(Point::new(2, 2));
+        let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2), (123, 456)]);
+
+        for (&a, b) in v.iter().zip(n) {
+            assert_eq!(a, b);
+        }
+    }
 }
--- a/rust/land2d/src/lib.rs	Thu Oct 18 22:23:25 2018 +0200
+++ b/rust/land2d/src/lib.rs	Thu Oct 18 22:34:09 2018 +0200
@@ -4,7 +4,7 @@
 use std::cmp;
 use std::ops;
 
-use integral_geometry::{Point, LinePoints};
+use integral_geometry::{LinePoints, ArcPoints, Point};
 
 pub struct Land2D<T> {
     pixels: vec2d::Vec2D<T>,
@@ -149,9 +149,15 @@
     }
 
     pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize
-        where I: std::iter::Iterator<Item = Point>
+    where
+        I: std::iter::Iterator<Item = Point>,
     {
-        i.map(|p| self.map(p.y, p.x, |v| {*v = value; 1})).count()
+        i.map(|p| {
+            self.map(p.y, p.x, |v| {
+                *v = value;
+                1
+            })
+        }).count()
     }
 
     pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {
@@ -279,9 +285,9 @@
         radius: i32,
         f: F,
     ) -> usize {
-        <Land2D<T>>::apply_around_circle(radius, &mut |dx, dy| {
-            self.fill_circle_lines(x, y, dx, dy, &f)
-        })
+        ArcPoints::new(radius)
+            .map(&mut |p: Point| self.fill_circle_lines(x, y, p.x, p.y, &f))
+            .sum()
     }
 
     #[inline]
@@ -307,13 +313,16 @@
 
     pub fn draw_thick_line(
         &mut self,
-        x1: i32,
-        y1: i32,
-        x2: i32,
-        y2: i32,
+        from: Point, to: Point,
         radius: i32,
         value: T,
     ) -> usize {
+        for deltas in ArcPoints::new(radius) {
+            for points in LinePoints::new(from, to) {
+
+            }
+        }
+
         <Land2D<T>>::apply_around_circle(radius, &mut |dx, dy| {
             <Land2D<T>>::apply_along_line(x1, y1, x2, y2, &mut |x, y| {
                 <Land2D<T>>::change_dots_around(x, y, dx, dy, &mut |x, y| {