master
E. Almqvist 3 years ago
parent 0c37c0745f
commit b1c945ffa0
  1. 14
      mas/fractals/mandel/src/main.rs
  2. 51
      mas/fractals/mandel/src/mandel.rs

@ -13,20 +13,18 @@ fn main() {
let height: u32 = args[2].parse::<u32>().unwrap(); let height: u32 = args[2].parse::<u32>().unwrap();
println!("{} {}", width, height); println!("{} {}", width, height);
*/ */
let (width, height) = (640, 480); let (width, height) = (1600, 900);
let (xr, yr) = ([-2.0, 0.47], [-2.12, 2.12]); let depth = 5000;
let theta = 0.01;
let scale = 100.0;
let depth = 400;
let (window, ctx, vid_sys) = render::create_window("Mandelbrot set", width, height); let (window, ctx, vid_sys) = render::create_window("Mandelbrot set", width, height);
let mut canvas = window.into_canvas().build().unwrap(); let mut canvas = window.into_canvas().build().unwrap();
canvas.set_draw_color(Color::RGB(255, 255, 255)); canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear(); canvas.clear();
canvas.present(); //canvas.present();
mandel::mandelbrot(&mut canvas, width, height, depth); println!("Rendering...");
mandel::mandelbrot(&mut canvas, width, height, depth, 0.0, 0.0, 0.0, 0.0);
canvas.present(); canvas.present();
println!("Post mandel render"); println!("Post mandel render");

@ -4,55 +4,18 @@ use num::complex::Complex64;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::render::Canvas; use sdl2::render::Canvas;
/*
#[derive(Debug)]
pub struct Color {
r: u8,
g: u8,
b: u8
}
impl Color {
pub fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}
*/
fn mandel(n: u64, c: Complex64) -> Complex64 {
if n == 0 {
return Complex64::new(0.0, 0.0);
} else {
return mandel(n-1, c).norm() + c;
}
}
pub fn get_point_color(x: f32, y: f32, depth: u64) -> Color {
let c = Complex64::new(x.into(), y.into());
let mut z = mandel(depth, c);
let mut norm = z.re*z.re - z.im*z.im;
//println!("{:?} {:?} < 2?", z, norm);
if norm < 4.0 {
return Color::RGB(0, 0, 0);
} else {
return Color::RGB(250, 250, 250);
}
}
pub fn get_col(i: u32, max_iter: u32) -> Color { pub fn get_col(i: u32, max_iter: u32) -> Color {
if i > max_iter || i == 255 { if i > max_iter || i == 255 {
return Color::RGB(255, 255, 255); return Color::RGB(255, 255, 255);
} else { } else {
return Color::RGB(i as u8, i as u8, i as u8); return Color::RGB(i as u8, 0, i as u8);
} }
} }
pub fn mandelbrot(canvas: &mut Canvas<sdl2::video::Window>, w: u32, h: u32, depth: u32) { pub fn mandelbrot(canvas: &mut Canvas<sdl2::video::Window>, w: u32, h: u32, depth: u32, xzoom: f32, yzoom: f32, xoffset: f32, yoffset: f32) {
/* /*
for each pixel (Px, Py) on the screen do for each pixel (Px, Py) on the screen do
x0 := scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.00, 0.47)) x0 := scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.00, 0.47))
y0 := scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1.12, 1.12)) y0 := scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1.12, 1.12))
x := 0.0 x := 0.0
@ -67,16 +30,14 @@ for each pixel (Px, Py) on the screen do
color := palette[iteration] color := palette[iteration]
plot(Px, Py, color) plot(Px, Py, color)
*/ */
let (mut x, mut y) = (0.0, 0.0);
for dx in 0..w { for dx in 0..w {
let x0 = ((dx as f32) / (w as f32)) * 3.5 - 2.5; let x0 = ((dx as f32) / (w as f32)) * (3.5 + xzoom) - (2.5 + xoffset);
for dy in 0..h { for dy in 0..h {
let y0 = ((dy as f32) / (h as f32)) * 2.0 - 1.0; let y0 = ((dy as f32) / (h as f32)) * (2.0 + yzoom) - (1.0 + yoffset);
let (mut x, mut y) = (0.0, 0.0);
let mut i: u32 = 0; let mut i: u32 = 0;
while i < depth && x*x + y*y <= 4.0 { while i < depth && x*x + y*y <= 4.0 {

Loading…
Cancel
Save