Mandelbrot ASCII

Published 330d ago

 ayebear

Mandelbrot ASCII, made by chatgpt 3.5

const WIDTH: usize = 100;
const HEIGHT: usize = 50;
const MAX_ITER: usize = 100;

fn mandelbrot(c_r: f64, c_i: f64) -> usize {
    let mut z_r = 0.0;
    let mut z_i = 0.0;
    let mut iter = 0;

    while z_r * z_r + z_i * z_i <= 4.0 && iter < MAX_ITER {
        let temp = z_r * z_r - z_i * z_i + c_r;
        z_i = 2.0 * z_r * z_i + c_i;
        z_r = temp;
        iter += 1;
    }

    iter
}

fn map_to_ascii(iter: usize) -> char {
    match iter % 8 {
        0 => ' ',
        1 => '.',
        2 => ',',
        3 => '~',
        4 => ':',
        5 => ';',
        6 => 'o',
        _ => 'O',
    }
}

fn main() {
    let mut buffer: Vec<char> = vec![' '; WIDTH * HEIGHT];

    for y in 0..HEIGHT {
        for x in 0..WIDTH {
            let c_r = (x as f64 * 3.0 / WIDTH as f64) - 2.2;
            let c_i = (y as f64 * 2.2 / HEIGHT as f64) - 1.1;

            let iter = mandelbrot(c_r, c_i);
            buffer[y * WIDTH + x] = map_to_ascii(iter);
        }
    }

    // Output to the terminal
    for y in 0..HEIGHT {
        for x in 0..WIDTH {
            print!("{}", buffer[y * WIDTH + x]);
        }
        println!();
    }
}

mandelbrot
1

Please login or sign up to comment and collaborate