Files
education-rs-rep/src/console_render/mod.rs
T
2026-05-11 14:29:27 +03:00

73 lines
2.0 KiB
Rust

use crossterm::{
//cursor::MoveTo,
cursor::MoveTo,
execute,
queue,
style::{Color, Print, ResetColor, SetBackgroundColor},
terminal::{Clear, ClearType},
};
use std::io::{Write, stdout};
mod c_math;
use c_math::*;
const X_SCAILE: f64 = 2.7;
pub fn m_render(
size: (usize, usize),
scale: f64,
pos: (f64, f64),
) -> Result<(), Box<dyn std::error::Error>> {
let mut grad = Color::Rgb { r: 0, g: 0, b: 0 };
let mut out = stdout();
queue!(out, MoveTo(0, 0))?;
//out.flush()?;
let mut z: f2d;
let mut c: f2d;
let xs: f64 = size.0 as f64 / 2.;
let ys: f64 = size.1 as f64 / 2.;
//let deep: f32 = 1.0 / scale;
let deep = (100.0 + scale.log10().abs() * 80.0) as usize;
for i in 0..size.0 {
for j in 0..size.1 {
let x: f64 = i as f64;
let y: f64 = j as f64;
c = f2d::new(
(x - xs) * scale as f64 * X_SCAILE + pos.0,
(y - ys) * scale as f64 + pos.1,
);
z = f2d::def();
let mut m: usize = 0;
let mut k = true;
while k {
z.mul(z.clone());
z.add(c.clone());
m += 1;
k = (z.len() < 4.0) && (m < deep);
}
if z.len() > 2.0 {
let smooth = (m as f64 + 1.0 - z.len().ln().ln() / 2.0_f64.ln()) as f32;
grad = Color::Rgb {
r: (((smooth * 0.1).sin() + 1.0) * 230.0) as u8,
g: (((smooth * 0.1).sin() + 1.0) * 100.0) as u8,
b: (((smooth * 0.1 + 0.1).sin() + 1.0) * 220.0) as u8,
};
queue!(out, SetBackgroundColor(grad), Print(" "), ResetColor)?;
out.flush()?;
grad = Color::Rgb { r: 0, g: 0, b: 0 };
} else {
queue!(out, SetBackgroundColor(grad), Print(" "), ResetColor,)?;
out.flush()?;
}
}
queue!(out, Print("\r\n"))?;
}
out.flush()?;
Ok(())
}