Better zooming

master
E. Almqvist 3 years ago
parent 7363674306
commit 7af4cc51ed
  1. 6
      mas/fractals/mandel/Cargo.toml
  2. 12
      mas/fractals/mandel/src/main.rs
  3. 4
      mas/fractals/mandel/src/mandel.rs
  4. 15
      mas/fractals/mandel/src/render.rs

@ -8,4 +8,8 @@ edition = "2021"
[dependencies] [dependencies]
num = "0.4.0" num = "0.4.0"
matrix = "0.22.0" matrix = "0.22.0"
sdl2 = "0.35.2"
[dependencies.sdl2]
version = "0.35.2"
default-features = false
features = ["ttf"]

@ -15,9 +15,9 @@ fn main() {
*/ */
let (width, height) = (640, 480); let (width, height) = (640, 480);
let (mut dx, mut dy) = (0.0, 0.0); let (mut dx, mut dy) = (0.0, 0.0);
let (mut zx, mut zy) = (0.0, 0.0); let (mut zx, mut zy) = (2.0, 2.0);
let mut step = 0.05; let mut step = 0.05;
let (mut dzx, mut dzy) = (0.4, 0.2); let (mut zoom_in, mut zoom_out) = (1.1, 0.9);
let depth = 1000; let depth = 1000;
let (window, ctx, vid_sys) = render::create_window("Mandelbrot set", width, height); let (window, ctx, vid_sys) = render::create_window("Mandelbrot set", width, height);
@ -60,14 +60,14 @@ fn main() {
Event::KeyDown { keycode: Some(Keycode::Space), .. } => { Event::KeyDown { keycode: Some(Keycode::Space), .. } => {
println!("ZOOM+"); println!("ZOOM+");
render_new = true; render_new = true;
zy -= dzy; zy *= zoom_in;
zx -= dzx; zx *= zoom_in;
}, },
Event::KeyDown { keycode: Some(Keycode::LShift), .. } => { Event::KeyDown { keycode: Some(Keycode::LShift), .. } => {
println!("ZOOM-"); println!("ZOOM-");
render_new = true; render_new = true;
zy += dzy; zy *= zoom_out;
zx += dzx; zx *= zoom_out;
}, },
_ => {} _ => {}
} }

@ -33,9 +33,9 @@ pub fn mandelbrot(canvas: &mut Canvas<sdl2::video::Window>, w: u32, h: u32, dept
*/ */
for dx in 0..w { for dx in 0..w {
let x0 = ((dx as f32) / (w as f32)) * (3.5 + xzoom) - (2.5 - xoffset); let x0 = ((dx as f32) / (w as f32)) * (xzoom) - (2.5 - xoffset);
for dy in 0..h { for dy in 0..h {
let y0 = ((dy as f32) / (h as f32)) * (2.0 + yzoom) - (1.0 + yoffset); let y0 = ((dy as f32) / (h as f32)) * (yzoom) - (1.0 + yoffset);
let (mut x, mut y) = (0.0, 0.0); let (mut x, mut y) = (0.0, 0.0);
let mut i: u32 = 0; let mut i: u32 = 0;

@ -2,6 +2,7 @@ extern crate sdl2;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::Rect; use sdl2::rect::Rect;
use sdl2::render::Canvas; use sdl2::render::Canvas;
use sdl2::ttf::Font;
pub fn create_window(title: &str, width: u32, height: u32) pub fn create_window(title: &str, width: u32, height: u32)
-> (sdl2::video::Window, sdl2::Sdl, sdl2::VideoSubsystem) -> (sdl2::video::Window, sdl2::Sdl, sdl2::VideoSubsystem)
@ -16,6 +17,20 @@ pub fn create_window(title: &str, width: u32, height: u32)
return (window, ctx, vid_sys); return (window, ctx, vid_sys);
} }
/*
pub fn text(x: u32, y: u32, text: &str, font_path: &str) {
let ttf_context = sdl2::ttf::init();
let mut font = ttf_context.load_font(font_path, 128);
font.set_style(sdl2::ttf::FontStyle::BOLD);
// render a surface, and convert it to a texture bound to the canvas
let surface = font
.render(text)
.blended(Color::RGBA(255, 255, 255, 255))
.map_err(|e| e.to_string())?;
}
*/
pub fn set_pixel(canvas: &mut Canvas<sdl2::video::Window>, x: i32, y: i32, color: Color) { pub fn set_pixel(canvas: &mut Canvas<sdl2::video::Window>, x: i32, y: i32, color: Color) {
canvas.set_draw_color(color); canvas.set_draw_color(color);
canvas.fill_rect(Rect::new(x, y, 1, 1)); canvas.fill_rect(Rect::new(x, y, 1, 1));

Loading…
Cancel
Save