add circle filling to land2d
authoralfadur
Tue, 30 Oct 2018 05:55:58 +0300
changeset 14052 c47283feafac
parent 14051 2ebd505e62c1
child 14053 2869c2ccb1b8
add circle filling to land2d
rust/integral-geometry/src/lib.rs
rust/land2d/src/lib.rs
rust/theme-editor/src/main.rs
--- a/rust/integral-geometry/src/lib.rs	Tue Oct 30 02:14:00 2018 +0300
+++ b/rust/integral-geometry/src/lib.rs	Tue Oct 30 05:55:58 2018 +0300
@@ -37,6 +37,12 @@
     pub fn max_norm(self) -> i32 {
         std::cmp::max(self.x.abs(), self.y.abs())
     }
+
+    #[inline]
+    pub fn transform(self, matrix: &[i32; 4]) -> Self {
+        Point::new(matrix[0] * self.x + matrix[1] * self.y,
+                   matrix[2] * self.x + matrix[3] * self.y)
+    }
 }
 
 macro_rules! bin_op_impl {
--- a/rust/land2d/src/lib.rs	Tue Oct 30 02:14:00 2018 +0300
+++ b/rust/land2d/src/lib.rs	Tue Oct 30 05:55:58 2018 +0300
@@ -219,6 +219,30 @@
             .sum()
     }
 
+    fn fill_row(&mut self, center: Point, offset: Point, value: T) -> usize {
+        let row_index = center.y + offset.y;
+        if self.is_valid_y(row_index) {
+            let from_x = cmp::max(0, center.x - offset.x) as usize;
+            let to_x = cmp::min(self.width() - 1, (center.x + offset.x) as usize);
+            self.pixels[row_index as usize][from_x..=to_x]
+                .iter_mut().for_each(|v| *v = value);
+            to_x - from_x + 1
+        } else {
+            0
+        }
+    }
+
+    pub fn fill_circle(&mut self, center: Point, radius: i32, value: T) -> usize {
+        let transforms =
+            [[0, 1, 1, 0], [0, 1, -1, 0],
+             [1, 0, 0, 1], [1, 0, 0, -1]];
+        ArcPoints::new(radius).map(|vector| {
+            transforms.iter().map(|m|
+                self.fill_row(center, vector.transform(m), value)
+            ).sum::<usize>()
+        }).sum()
+    }
+
     pub fn draw_thick_line(&mut self, from: Point, to: Point, radius: i32, value: T) -> usize {
         let mut result = 0;
 
--- a/rust/theme-editor/src/main.rs	Tue Oct 30 02:14:00 2018 +0300
+++ b/rust/theme-editor/src/main.rs	Tue Oct 30 05:55:58 2018 +0300
@@ -83,8 +83,10 @@
     }
 
     let mut land = Land2D::new(WIDTH as usize, HEIGHT as usize, 0);
-    for i in 0..64 {
+    for i in 0..32 {
         land.draw_thick_line(point(), point(), rnd(5), u32::max_value());
+
+        land.fill_circle(point(), rnd(60), u32::max_value());
     }
 
     fill_texture(&mut land_surf, &land);