#![allow(clippy::many_single_char_names)] use crate::{colors::generate_palette, texture::Texture, vec3::Vec3}; #[derive(Debug)] pub struct Mandelbrot { palette: Vec, } impl Default for Mandelbrot { fn default() -> Self { Mandelbrot { palette: generate_palette(10), } } } impl Texture for Mandelbrot { fn value(&self, u: f32, v: f32, _p: Vec3) -> Vec3 { // scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1)) let x0 = u * 3.5 - 2.5; // scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1)) let y0 = v * 2.0 - 1.0; let mut x = 0.0; let mut y = 0.0; let mut iteration = 0; let max_iteration = 1000; while (x * x + y * y) <= 2. * 2. && iteration < max_iteration { let xtemp = x * x - y * y + x0; y = 2. * x * y + y0; x = xtemp; iteration += 1; } if iteration == max_iteration { return Vec3::default(); } self.palette[iteration % self.palette.len()] } }