Skip to content
Snippets Groups Projects
Commit 4b6f553e authored by Finn Bear's avatar Finn Bear
Browse files

Fractal slide improvements.

parent 07f64793
No related branches found
No related tags found
No related merge requests found
...@@ -87,6 +87,7 @@ impl Code { ...@@ -87,6 +87,7 @@ impl Code {
/// Note: Must use non-idiomatic `return x;` instead of just `x`. /// Note: Must use non-idiomatic `return x;` instead of just `x`.
pub fn pseudocode(code: &str) -> String { pub fn pseudocode(code: &str) -> String {
code.replace("#[rustfmt::skip]\n", "") code.replace("#[rustfmt::skip]\n", "")
.replace("#[allow(unused)]\n", "")
.replace("pub ", "") .replace("pub ", "")
.replace("fn ", "function ") .replace("fn ", "function ")
.replace("let ", "") .replace("let ", "")
......
mod circle; mod circle;
mod dummy;
mod julia_gradient; mod julia_gradient;
mod mandelbrot; mod mandelbrot;
mod mandelbrot_gradient; mod mandelbrot_gradient;
...@@ -13,6 +14,7 @@ use crate::fade_in::{fade_in_manual, fade_out_manual}; ...@@ -13,6 +14,7 @@ use crate::fade_in::{fade_in_manual, fade_out_manual};
use crate::governor::Governor; use crate::governor::Governor;
use crate::slide::s4_automata::randomize_grid; use crate::slide::s4_automata::randomize_grid;
use crate::slide::s6_fractals::circle::circle; 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::julia_gradient::julia as julia_gradient;
use crate::slide::s6_fractals::mandelbrot::mandelbrot; use crate::slide::s6_fractals::mandelbrot::mandelbrot;
use crate::slide::s6_fractals::mandelbrot_gradient::mandelbrot as mandelbrot_gradient; use crate::slide::s6_fractals::mandelbrot_gradient::mandelbrot as mandelbrot_gradient;
...@@ -87,6 +89,7 @@ impl Default for Fractals { ...@@ -87,6 +89,7 @@ impl Default for Fractals {
let mut code = Code::default(); let mut code = Code::default();
code.name = "fractal_code"; code.name = "fractal_code";
code.offset_x = -HORIZONTAL_OFFSET; code.offset_x = -HORIZONTAL_OFFSET;
code.code = pseudocode(include_str!("s6_fractals/dummy.rs"));
// Grid goes on the right. // Grid goes on the right.
let mut grid = Grid::default(); let mut grid = Grid::default();
...@@ -170,7 +173,7 @@ impl Slide for Fractals { ...@@ -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 // 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. // 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. // 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 { fn wrap_bool_algo<A: Fn(f32, f32) -> bool>(a: A) -> impl Fn(f32, f32) -> Color32 {
...@@ -188,7 +191,7 @@ impl Slide for Fractals { ...@@ -188,7 +191,7 @@ impl Slide for Fractals {
move |x: f32, y: f32| { move |x: f32, y: f32| {
let c = (a(x, y) * 255.0) as u8; let c = (a(x, y) * 255.0) as u8;
// Slight bluish tint. // 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 { ...@@ -196,6 +199,9 @@ impl Slide for Fractals {
// We use a mutable reference so we can increment in one place. // We use a mutable reference so we can increment in one place.
let mut limit_pixels: Option<&mut usize> = None; let mut limit_pixels: Option<&mut usize> = None;
// Whether to skip rendering and clearing entirely.
let mut skip = false;
match &mut self.state { match &mut self.state {
FractalsState::Before => { FractalsState::Before => {
// Don't proceed to render the code/grid. // Don't proceed to render the code/grid.
...@@ -207,6 +213,7 @@ impl Slide for Fractals { ...@@ -207,6 +213,7 @@ impl Slide for Fractals {
self.code.alpha = alpha; self.code.alpha = alpha;
self.grid.alpha = alpha; self.grid.alpha = alpha;
}); });
skip = true;
} }
&mut FractalsState::Erase { fade_start } => { &mut FractalsState::Erase { fade_start } => {
fade_out_manual(ui, fade_start, |_, alpha| { fade_out_manual(ui, fade_start, |_, alpha| {
...@@ -217,18 +224,19 @@ impl Slide for Fractals { ...@@ -217,18 +224,19 @@ impl Slide for Fractals {
} }
} }
}); });
skip = true;
} }
&mut FractalsState::Axes { .. } => { &mut FractalsState::Axes { .. } => {
// No-op (arrows are rendered below). skip = true;
} }
FractalsState::Rectangle { pixels } => { FractalsState::Rectangle { pixels } => {
self.code.code = pseudocode(include_str!("s6_fractals/rectangle.rs")); 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) limit_pixels = Some(pixels)
} }
FractalsState::Circle { pixels } => { FractalsState::Circle { pixels } => {
self.code.code = pseudocode(include_str!("s6_fractals/circle.rs")); 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); limit_pixels = Some(pixels);
} }
FractalsState::Mandelbrot { FractalsState::Mandelbrot {
...@@ -236,7 +244,7 @@ impl Slide for Fractals { ...@@ -236,7 +244,7 @@ impl Slide for Fractals {
traversal: _, traversal: _,
} => { } => {
self.code.code = pseudocode(include_str!("s6_fractals/mandelbrot.rs")); 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); limit_pixels = Some(pixels);
} }
FractalsState::MandelbrotGradient { pixels } => { FractalsState::MandelbrotGradient { pixels } => {
...@@ -244,12 +252,12 @@ impl Slide for Fractals { ...@@ -244,12 +252,12 @@ impl Slide for Fractals {
self.grid.size_cells = 256; self.grid.size_cells = 256;
self.code.code = pseudocode(include_str!("s6_fractals/mandelbrot_gradient.rs")); 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); limit_pixels = Some(pixels);
} }
FractalsState::JuliaGradient { pixels } => { FractalsState::JuliaGradient { pixels } => {
self.code.code = pseudocode(include_str!("s6_fractals/julia_gradient.rs")); 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); limit_pixels = Some(pixels);
} }
} }
...@@ -260,9 +268,6 @@ impl Slide for Fractals { ...@@ -260,9 +268,6 @@ impl Slide for Fractals {
// Which pixels should be set to transparent. // Which pixels should be set to transparent.
let mut clear_range: (Bound<usize>, Bound<usize>) = (Bound::Unbounded, Bound::Unbounded); 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. // Double mutable borrow to avoid moving limit_pixels.
if let Some(limit_pixels) = &mut limit_pixels { if let Some(limit_pixels) = &mut limit_pixels {
if **limit_pixels < self.grid.size_cells.pow(2) if **limit_pixels < self.grid.size_cells.pow(2)
...@@ -288,24 +293,22 @@ impl Slide for Fractals { ...@@ -288,24 +293,22 @@ impl Slide for Fractals {
// Paint the grid. // Paint the grid.
if !skip { if !skip {
if let Some(algo) = algo { // Store the resolution before mutably borrowing self.grid.
// Store the resolution before mutably borrowing self.grid. let size_cells = self.grid.size_cells;
let size_cells = self.grid.size_cells;
for (i, (gx, gy, c)) in self.grid.iter_mut().enumerate() {
for (i, (gx, gy, c)) in self.grid.iter_mut().enumerate() { if render_range.contains(&i) {
if render_range.contains(&i) { // 0 to 1.
// 0 to 1. let nx = (gx as f32 + 0.5) / size_cells as f32;
let nx = (gx as f32 + 0.5) / size_cells as f32; let ny = (gy as f32 + 0.5) / size_cells as f32;
let ny = (gy as f32 + 0.5) / size_cells as f32;
// -4 to 4.
// -4 to 4. let x = nx * 4.0 - 2.0;
let x = nx * 4.0 - 2.0; let y = ny * 4.0 - 2.0;
let y = ny * 4.0 - 2.0;
*c = algo(x, y);
*c = algo(x, y); } else if clear_range.contains(&i) {
} else if clear_range.contains(&i) { *c = Color32::TRANSPARENT;
*c = Color32::TRANSPARENT;
}
} }
} }
} }
......
#[rustfmt::skip]
#[allow(unused)]
pub fn is_black(x: f32, y: f32) -> bool {
return false;
}
#[rustfmt::skip] #[rustfmt::skip]
pub fn julia(mut x: f32, mut y: f32) -> f32 { pub fn julia(mut x: f32, mut y: f32) -> f32 {
let max = 200.0; let max = 128.0;
let mut i = 0.0; let mut i = 0.0;
while x * x + y * y while x * x + y * y
......
#[rustfmt::skip] #[rustfmt::skip]
pub fn mandelbrot(x: f32, y: f32) -> f32 { pub fn mandelbrot(x: f32, y: f32) -> f32 {
let max = 100.0; let max = 80.0;
let mut x1 = 0.0; let mut x1 = 0.0;
let mut y1 = 0.0; let mut y1 = 0.0;
let mut i = 0.0; let mut i = 0.0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment