RetroCoders Community

Other Languages Programming => Rust => Topic started by: ron77 on Dec 27, 2024, 01:52 PM

Title: Rust Donut
Post by: ron77 on Dec 27, 2024, 01:52 PM
use std::{thread, time::Duration};

fn main() {
    let mut a = 0.0f32;
    let mut b = 0.0f32;
    let mut z = vec![0.0f32; 1760];
    let mut buffer = vec![' '; 1760];
   
    // Clear screen
    print!("\x1b[2J");
   
    loop {
        // Reset buffers
        buffer.fill(' ');
        z.fill(0.0);
       
        let mut j = 0.0f32;
        while j < 6.28 {
            let mut i = 0.0f32;
            while i < 6.28 {
                let c = i.sin();
                let d = j.cos();
                let e = a.sin();
                let f = j.sin();
                let g = a.cos();
                let h = d + 2.0;
                let d = 1.0 / (c * h * e + f * g + 5.0);
                let l = i.cos();
                let m = b.cos();
                let n = b.sin();
                let t = c * h * g - f * e;
                let x = (40.0 + 30.0 * d * (l * h * m - t * n)) as i32;
                let y = (12.0 + 15.0 * d * (l * h * n + t * m)) as i32;
                let o = (x + 80 * y) as usize;
                let luminance = (8.0 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n)) as i32;
               
                if y > 0 && y < 22 && x > 0 && x < 80 && d > z[o] {
                    z[o] = d;
                    buffer[o] = ".,-~:;=!*#$@".chars().nth((luminance.max(0) as usize).min(11)).unwrap();
                }
               
                i += 0.02;
            }
            j += 0.07;
        }
       
        // Move cursor to home position
        print!("\x1b[H");
       
        // Print the frame
        for k in 0..1760 {
            if k % 80 == 0 {
                println!();
            } else {
                print!("{}", buffer[k]);
            }
        }
       
        a += 0.04;
        b += 0.02;
        thread::sleep(Duration::from_micros(30000));
    }

}