Wikipedia method

master
E. Almqvist 3 years ago
parent fea3a761c4
commit 0c37c0745f
  1. 22
      mas/fractals/mandel/src/main.rs
  2. 27
      mas/fractals/mandel/src/mandel.rs

@ -26,25 +26,9 @@ fn main() {
canvas.clear();
canvas.present();
mandel::mandelbrot(&mut canvas, width, height, depth);
canvas.present();
let (mut dx, mut dy) = (xr[0], yr[0]);
let (mut x, mut y) = (0.0, 0.0);
let col: Color; // color buffer
while dx <= xr[1] {
dy = yr[0];
while dy <= yr[1] {
let col: Color = mandel::get_point_color(dx, dy, depth);
y = dy*scale + (height as f32)/2.0;
x = dx*scale + (width as f32)/2.0;
// render pixel
render::set_pixel(&mut canvas, x as i32, y as i32, col);
println!("({dx} {dy}) - {x} {y}: {col:?}");
dy += theta;
}
dx += theta;
canvas.present();
}
println!("Post mandel render");
let mut event_pump = ctx.event_pump().unwrap();
@ -61,7 +45,5 @@ fn main() {
}
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
let mut coltest: Color = mandel::get_point_color(0.0, 1.0, depth);
println!("{coltest:?}");
}

@ -1,5 +1,8 @@
use crate::render::set_pixel;
use num::complex::Complex64;
use sdl2::pixels::Color;
use sdl2::render::Canvas;
/*
#[derive(Debug)]
@ -38,7 +41,15 @@ pub fn get_point_color(x: f32, y: f32, depth: u64) -> Color {
}
}
pub fn mandelbrot(w: u32, h: u32) -> Canvas {
pub fn get_col(i: u32, max_iter: u32) -> Color {
if i > max_iter || i == 255 {
return Color::RGB(255, 255, 255);
} else {
return Color::RGB(i as u8, i as u8, i as u8);
}
}
pub fn mandelbrot(canvas: &mut Canvas<sdl2::video::Window>, w: u32, h: u32, depth: u32) {
/*
for each pixel (Px, Py) on the screen do
@ -59,9 +70,23 @@ for each pixel (Px, Py) on the screen do
*/
let (mut x, mut y) = (0.0, 0.0);
for dx in 0..w {
let x0 = ((dx as f32) / (w as f32)) * 3.5 - 2.5;
for dy in 0..h {
let y0 = ((dy as f32) / (h as f32)) * 2.0 - 1.0;
let mut i: u32 = 0;
while i < depth && x*x + y*y <= 4.0 {
let xtemp = x*x - y*y + x0;
y = 2.0*x*y + y0;
x = xtemp;
i += 1;
}
let col = get_col(i, depth);
set_pixel(canvas, dx as i32, dy as i32, col);
}
}
}

Loading…
Cancel
Save