diff --git a/src/main.rs b/src/main.rs index bbdb168..508fa9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,13 +138,9 @@ fn mandelbrot_render_system( let y = ((-1.0 * z) - v, (1.0 * z) - v); let bounds = Bounds::new(x, y); - let start = Instant::now(); - texture .data .copy_from_slice(fractal.scaled_image(bounds, limit)); - - let _diff = Instant::now() - start; } } } diff --git a/src/mandelbrot.rs b/src/mandelbrot.rs index 90473d1..b6130cf 100644 --- a/src/mandelbrot.rs +++ b/src/mandelbrot.rs @@ -65,52 +65,56 @@ impl Default for Mandelbrot { impl Mandelbrot { pub fn image(&mut self) -> &[u8] { let limit = DEFAULT_ITERATION_LIMIT as f64; - let error = f64::EPSILON; self.frame_buffer .par_chunks_mut(4) .enumerate() .for_each(|(i, buf)| { let iters = Self::escape_time(i, DEFAULT_BOUNDS, DEFAULT_ITERATION_LIMIT); - let normalized = iters / limit; - let h = normalized * 350.0; - let v = if (iters - limit).abs() < error { - 0.0 - } else { - 1.0 - }; - - buf.copy_from_slice(&Self::hsv_to_rgb(h, 1.0, v)); + buf.copy_from_slice(&Self::default_colours(iters, limit)) }); self.frame_buffer.as_ref() } pub fn scaled_image(&mut self, bounds: Bounds, limit: u32) -> &[u8] { - let error = f64::EPSILON; - self.frame_buffer .par_chunks_mut(4) .enumerate() .for_each(|(i, buf)| { let iters = Self::escape_time(i, bounds, limit); let limit = limit as f64; - let normalized = iters / limit; - let h = normalized * 350.0; - let v = if (iters - limit).abs() < error { - 0.0 - } else { - 1.0 - }; - - buf.copy_from_slice(&Self::hsv_to_rgb(h, 1.0, v)) + buf.copy_from_slice(&Self::default_colours(iters, limit)) }); self.frame_buffer.as_ref() } + fn olc_colours(iters: f64) -> [u8; 4] { + let a = 0.1; + + let red = (0.5 * (a * iters).sin() + 0.5) * 255.0; + let green = (0.5 * (a * iters + 2.094).sin() + 0.5) * 255.0; + let blue = (0.5 * (a * iters + 4.188).sin() + 0.5) * 255.0; + + [red as u8, green as u8, blue as u8, 0xFF] + } + + fn default_colours(iters: f64, limit: f64) -> [u8; 4] { + let normal = iters / limit; + + let h = normal * 350.0; + let v = if (iters - limit).abs() < f64::EPSILON { + 0.0 + } else { + 1.0 + }; + + Self::hsv_to_rgb(h, 1.0, v) + } + fn escape_time(i: usize, bounds: Bounds, limit: u32) -> f64 { let point = Coordinate::new(i % TEXTURE_WIDTH, i / TEXTURE_WIDTH); let c = point.into_complex(bounds); @@ -118,7 +122,6 @@ impl Mandelbrot { Self::count_iterations(c, limit) } - #[inline] fn hsv_to_rgb(h: f64, s: f64, v: f64) -> [u8; 4] { let c = v * s; let x = c * (1.0 - (((h / 60.0) % 2.0) - 1.0).abs()); @@ -167,6 +170,8 @@ impl Mandelbrot { count += 1; } + // For a performance booth (with the consequence of a less smooth gradient) + // just return count instead of doing all this math on it if count < limit { (count as f64 + 1.0) - z.norm().ln().ln() / 2f64.ln() } else { @@ -174,7 +179,6 @@ impl Mandelbrot { } } - #[inline] fn scale_width(x: usize, bounds: (f64, f64)) -> f64 { // const X_MIN: f64 = -2.5; // const X_MAX: f64 = 1.0; @@ -182,7 +186,6 @@ impl Mandelbrot { bounds.0 + ((bounds.1 - bounds.0) * (x as f64 - 0.0)) / TEXTURE_WIDTH as f64 - 0.0 } - #[inline] fn scale_height(y: usize, bounds: (f64, f64)) -> f64 { // const Y_MIN: f64 = -1.0; // const Y_MAX: f64 = 1.0;