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

@ -13,20 +13,18 @@ fn main() {
let height: u32 = args[2].parse::<u32>().unwrap();
println!("{} {}", width, height);
*/
let (width, height) = (640, 480);
let (xr, yr) = ([-2.0, 0.47], [-2.12, 2.12]);
let theta = 0.01;
let scale = 100.0;
let depth = 400;
let (width, height) = (1600, 900);
let depth = 5000;
let (window, ctx, vid_sys) = render::create_window("Mandelbrot set", width, height);
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.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();
println!("Post mandel render");

@ -4,79 +4,40 @@ use num::complex::Complex64;
use sdl2::pixels::Color;
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 {
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);
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
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))
x := 0.0
y := 0.0
iteration := 0
max_iteration := 1000
while (x*x + y*y 2*2 AND iteration < max_iteration) do
xtemp := x*x - y*y + x0
y := 2*x*y + y0
x := xtemp
iteration := iteration + 1
color := palette[iteration]
plot(Px, Py, color)
*/
let (mut x, mut y) = (0.0, 0.0);
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))
y0 := scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1.12, 1.12))
x := 0.0
y := 0.0
iteration := 0
max_iteration := 1000
while (x*x + y*y 2*2 AND iteration < max_iteration) do
xtemp := x*x - y*y + x0
y := 2*x*y + y0
x := xtemp
iteration := iteration + 1
color := palette[iteration]
plot(Px, Py, color)
*/
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 {
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;
while i < depth && x*x + y*y <= 4.0 {

Loading…
Cancel
Save