diff --git a/src/component/code.rs b/src/component/code.rs index a804ced2676b70773a451d9706e1cea2fc98727f..79d210ab1b21ff781621180451676c90732a0be6 100644 --- a/src/component/code.rs +++ b/src/component/code.rs @@ -87,6 +87,7 @@ impl Code { /// Note: Must use non-idiomatic `return x;` instead of just `x`. pub fn pseudocode(code: &str) -> String { code.replace("#[rustfmt::skip]\n", "") + .replace("#[allow(unused)]\n", "") .replace("pub ", "") .replace("fn ", "function ") .replace("let ", "") diff --git a/src/slide/s6_fractals.rs b/src/slide/s6_fractals.rs index dbeed834ea33f32cc8a2fc1d7578d965420897e4..b67ab3be9494a8e04dd30c74a7473853c4bdb0ba 100644 --- a/src/slide/s6_fractals.rs +++ b/src/slide/s6_fractals.rs @@ -1,4 +1,5 @@ mod circle; +mod dummy; mod julia_gradient; mod mandelbrot; mod mandelbrot_gradient; @@ -13,6 +14,7 @@ use crate::fade_in::{fade_in_manual, fade_out_manual}; use crate::governor::Governor; use crate::slide::s4_automata::randomize_grid; use crate::slide::s6_fractals::circle::circle; +use crate::slide::s6_fractals::dummy::is_black; use crate::slide::s6_fractals::julia_gradient::julia as julia_gradient; use crate::slide::s6_fractals::mandelbrot::mandelbrot; use crate::slide::s6_fractals::mandelbrot_gradient::mandelbrot as mandelbrot_gradient; @@ -87,6 +89,7 @@ impl Default for Fractals { let mut code = Code::default(); code.name = "fractal_code"; code.offset_x = -HORIZONTAL_OFFSET; + code.code = pseudocode(include_str!("s6_fractals/dummy.rs")); // Grid goes on the right. let mut grid = Grid::default(); @@ -170,7 +173,7 @@ impl Slide for Fractals { // // Note that we use indirection via a reference but not a heap-allocated [`Box`]. We can get // away with that since the function doesn't leave our stack frame. - let mut algo: Option<Box<dyn Fn(f32, f32) -> Color32>> = None; + let mut algo: Box<dyn Fn(f32, f32) -> Color32> = Box::new(wrap_bool_algo(is_black)); // Don't necessarily need to understand what is going on here. fn wrap_bool_algo<A: Fn(f32, f32) -> bool>(a: A) -> impl Fn(f32, f32) -> Color32 { @@ -188,7 +191,7 @@ impl Slide for Fractals { move |x: f32, y: f32| { let c = (a(x, y) * 255.0) as u8; // Slight bluish tint. - Color32::from_rgb((c as u16 * 8 / 10) as u8, (c as u16 * 9 / 10) as u8, c) + Color32::from_rgb((c as u16 * 9 / 10) as u8, (c as u16 * 19 / 20) as u8, c) } } @@ -196,6 +199,9 @@ impl Slide for Fractals { // We use a mutable reference so we can increment in one place. let mut limit_pixels: Option<&mut usize> = None; + // Whether to skip rendering and clearing entirely. + let mut skip = false; + match &mut self.state { FractalsState::Before => { // Don't proceed to render the code/grid. @@ -207,6 +213,7 @@ impl Slide for Fractals { self.code.alpha = alpha; self.grid.alpha = alpha; }); + skip = true; } &mut FractalsState::Erase { fade_start } => { fade_out_manual(ui, fade_start, |_, alpha| { @@ -217,18 +224,19 @@ impl Slide for Fractals { } } }); + skip = true; } &mut FractalsState::Axes { .. } => { - // No-op (arrows are rendered below). + skip = true; } FractalsState::Rectangle { pixels } => { self.code.code = pseudocode(include_str!("s6_fractals/rectangle.rs")); - algo = Some(Box::new(wrap_bool_algo(rectangle))); + algo = Box::new(wrap_bool_algo(rectangle)); limit_pixels = Some(pixels) } FractalsState::Circle { pixels } => { self.code.code = pseudocode(include_str!("s6_fractals/circle.rs")); - algo = Some(Box::new(wrap_bool_algo(circle))); + algo = Box::new(wrap_bool_algo(circle)); limit_pixels = Some(pixels); } FractalsState::Mandelbrot { @@ -236,7 +244,7 @@ impl Slide for Fractals { traversal: _, } => { self.code.code = pseudocode(include_str!("s6_fractals/mandelbrot.rs")); - algo = Some(Box::new(wrap_bool_algo(mandelbrot))); + algo = Box::new(wrap_bool_algo(mandelbrot)); limit_pixels = Some(pixels); } FractalsState::MandelbrotGradient { pixels } => { @@ -244,12 +252,12 @@ impl Slide for Fractals { self.grid.size_cells = 256; self.code.code = pseudocode(include_str!("s6_fractals/mandelbrot_gradient.rs")); - algo = Some(Box::new(wrap_f32_algo(mandelbrot_gradient))); + algo = Box::new(wrap_f32_algo(mandelbrot_gradient)); limit_pixels = Some(pixels); } FractalsState::JuliaGradient { pixels } => { self.code.code = pseudocode(include_str!("s6_fractals/julia_gradient.rs")); - algo = Some(Box::new(wrap_f32_algo(julia_gradient))); + algo = Box::new(wrap_f32_algo(julia_gradient)); limit_pixels = Some(pixels); } } @@ -260,9 +268,6 @@ impl Slide for Fractals { // Which pixels should be set to transparent. let mut clear_range: (Bound<usize>, Bound<usize>) = (Bound::Unbounded, Bound::Unbounded); - // Whether to skip rendering and clearing entirely. - let mut skip = false; - // Double mutable borrow to avoid moving limit_pixels. if let Some(limit_pixels) = &mut limit_pixels { if **limit_pixels < self.grid.size_cells.pow(2) @@ -288,24 +293,22 @@ impl Slide for Fractals { // Paint the grid. if !skip { - if let Some(algo) = algo { - // Store the resolution before mutably borrowing self.grid. - let size_cells = self.grid.size_cells; - - for (i, (gx, gy, c)) in self.grid.iter_mut().enumerate() { - if render_range.contains(&i) { - // 0 to 1. - let nx = (gx as f32 + 0.5) / size_cells as f32; - let ny = (gy as f32 + 0.5) / size_cells as f32; - - // -4 to 4. - let x = nx * 4.0 - 2.0; - let y = ny * 4.0 - 2.0; - - *c = algo(x, y); - } else if clear_range.contains(&i) { - *c = Color32::TRANSPARENT; - } + // Store the resolution before mutably borrowing self.grid. + let size_cells = self.grid.size_cells; + + for (i, (gx, gy, c)) in self.grid.iter_mut().enumerate() { + if render_range.contains(&i) { + // 0 to 1. + let nx = (gx as f32 + 0.5) / size_cells as f32; + let ny = (gy as f32 + 0.5) / size_cells as f32; + + // -4 to 4. + let x = nx * 4.0 - 2.0; + let y = ny * 4.0 - 2.0; + + *c = algo(x, y); + } else if clear_range.contains(&i) { + *c = Color32::TRANSPARENT; } } } diff --git a/src/slide/s6_fractals/dummy.rs b/src/slide/s6_fractals/dummy.rs new file mode 100644 index 0000000000000000000000000000000000000000..9e6159cab2b339d7a907d734bbd53befad602cd7 --- /dev/null +++ b/src/slide/s6_fractals/dummy.rs @@ -0,0 +1,5 @@ +#[rustfmt::skip] +#[allow(unused)] +pub fn is_black(x: f32, y: f32) -> bool { + return false; +} diff --git a/src/slide/s6_fractals/julia_gradient.rs b/src/slide/s6_fractals/julia_gradient.rs index fa4992de554403a7fc7f7b86141ffac5cea4948e..8f3ddac25c57224a420205826d22dd3b0fa54aba 100644 --- a/src/slide/s6_fractals/julia_gradient.rs +++ b/src/slide/s6_fractals/julia_gradient.rs @@ -1,6 +1,6 @@ #[rustfmt::skip] pub fn julia(mut x: f32, mut y: f32) -> f32 { - let max = 200.0; + let max = 128.0; let mut i = 0.0; while x * x + y * y diff --git a/src/slide/s6_fractals/mandelbrot_gradient.rs b/src/slide/s6_fractals/mandelbrot_gradient.rs index 602b584088fb874ed79b3990ec9d387d2f950d15..76f9ce5f3bfb6584365717ff3df89a73eb4fa69e 100644 --- a/src/slide/s6_fractals/mandelbrot_gradient.rs +++ b/src/slide/s6_fractals/mandelbrot_gradient.rs @@ -1,6 +1,6 @@ #[rustfmt::skip] pub fn mandelbrot(x: f32, y: f32) -> f32 { - let max = 100.0; + let max = 80.0; let mut x1 = 0.0; let mut y1 = 0.0; let mut i = 0.0;