Bill Thiede e19ec20c7b rtiow: Many changes
Add debugging hit DebugHit.
Move some color helpers from mandelbrot to colors mod.
Implement Texture trait for [f32;3]
2023-02-13 20:57:10 -08:00

40 lines
1.1 KiB
Rust

#![allow(clippy::many_single_char_names)]
use crate::{colors::generate_palette, texture::Texture, vec3::Vec3};
#[derive(Debug)]
pub struct Mandelbrot {
palette: Vec<Vec3>,
}
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()]
}
}