master
E. Almqvist 3 years ago
parent d002c663e3
commit 047eae1d9e
  1. 15
      mas/fractals/mandel/src/main.rs
  2. 5
      mas/fractals/mandel/src/mandel.rs
  3. 14
      mas/fractals/mandel/src/render.rs

@ -1,8 +1,6 @@
mod mandel;
mod render;
use std::env;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
@ -17,9 +15,14 @@ fn main() {
*/
let (width, height) = (640, 480);
let (xr, yr) = ([-200, 200], [-200, 200]);
let depth = 500;
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.clear();
canvas.present();
let mut event_pump = ctx.event_pump().unwrap();
'running: loop {
@ -34,6 +37,14 @@ fn main() {
}
// render stuff
for dx in xr[0]..xr[1] {
for dy in yr[0]..yr[1] {
let col: Color = mandel::get_point_color(width.try_into().unwrap(), height.try_into().unwrap(), dx, dy, depth);
println!("{}:{} {:?}", dx, dy, col);
render::set_pixel(&mut canvas, dx, dy, col);
}
}
canvas.present();
}
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}

@ -1,5 +1,4 @@
use num::complex::Complex;
use matrix::Matrix;
use sdl2::pixels::Color;
/*
@ -29,13 +28,13 @@ fn mandel(n: u64, c: Complex<f64>) -> Complex<f64> {
}
pub fn get_point_color(w: i32, h: i32, x: i32, y: i32, depth: u64) -> Color {
let c = Complex::<f64>::new((x - w/2).into(), (y - h/2).into());
let c = Complex::<f64>::new((x + w/2).into(), (y + w/2).into());
let mut norm = mandel(depth, c).norm();
if norm < 2.0 {
return Color::RGB(0, 0, 0);
} else {
return Color::RGB(255, 255, 255);
return Color::RGB(255, 25, 25);
}
}

@ -1,12 +1,11 @@
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;
use sdl2::rect::Rect;
use sdl2::render::Canvas;
pub fn create_window(title: &str, width: u32, height: u32)
-> (sdl2::video::Window, sdl2::Sdl, sdl2::VideoSubsystem) {
-> (sdl2::video::Window, sdl2::Sdl, sdl2::VideoSubsystem)
{
let ctx = sdl2::init().unwrap();
let vid_sys = ctx.video().unwrap();
@ -16,3 +15,8 @@ pub fn create_window(title: &str, width: u32, height: u32)
.unwrap();
return (window, ctx, vid_sys);
}
pub fn set_pixel(canvas: &mut Canvas<sdl2::video::Window>, x: i32, y: i32, color: Color) {
canvas.set_draw_color(color);
canvas.fill_rect(Rect::new(x, y, 1, 1));
}

Loading…
Cancel
Save