From 8b63d4fd7ac620910f3594703335c8f582a7c679 Mon Sep 17 00:00:00 2001 From: lemon-cat Date: Wed, 6 May 2026 17:57:47 +0300 Subject: [PATCH] lesson 2 --- Cargo.toml | 1 + console_render/mod.rs | 0 src/console_render/c_math.rs | 35 ++++++++++++ src/console_render/mod.rs | 68 +++++++++++++++++++++++ src/main.rs | 104 +++++++++++++++++++++++++++-------- src/my_math/mod.rs | 41 -------------- 6 files changed, 185 insertions(+), 64 deletions(-) create mode 100644 console_render/mod.rs create mode 100644 src/console_render/c_math.rs create mode 100644 src/console_render/mod.rs delete mode 100644 src/my_math/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 20063c1..94fb773 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +crossterm = "0.29" diff --git a/console_render/mod.rs b/console_render/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/console_render/c_math.rs b/src/console_render/c_math.rs new file mode 100644 index 0000000..12d5f07 --- /dev/null +++ b/src/console_render/c_math.rs @@ -0,0 +1,35 @@ +pub fn mid(a: f32, b: f32) -> f32 { + (a + b) / 2.0 +} + +#[derive(Clone)] +pub struct f2d { + x: f32, + y: f32, +} + +impl f2d { + pub fn new(a: f32, b: f32) -> Self { + f2d { x: a, y: b } + } + pub fn def() -> Self { + f2d { x: 0.0, y: 0.0 } + } + // + pub fn len(&self) -> f32 { + (self.x * self.x + self.y * self.y).sqrt() + } + + pub fn add(&mut self, b: f2d) { + self.x += b.x; + self.y += b.y; + } + + pub fn mul(&mut self, b: f2d) { + let x: f32 = self.x * b.x - self.y * b.y; + let y: f32 = self.x * b.y + self.y * b.x; + + self.x = x; + self.y = y; + } +} diff --git a/src/console_render/mod.rs b/src/console_render/mod.rs new file mode 100644 index 0000000..1c30d93 --- /dev/null +++ b/src/console_render/mod.rs @@ -0,0 +1,68 @@ +use crossterm::{ + cursor::MoveTo, + execute, + style::{Color, Print, ResetColor, SetBackgroundColor}, +}; +use std::io::stdout; + +mod c_math; +use c_math::*; + +const X_SCAILE: f32 = 2.0; +pub fn m_render( + size: (usize, usize), + scale: f32, + pos: (f32, f32), +) -> Result<(), Box> { + let mut colors = [ + Color::Rgb { r: 0, g: 0, b: 0 }, + Color::Rgb { + r: 150, + g: 90, + b: 150, + }, + ]; + let mut out = stdout(); + + let mut z: f2d; + let mut c: f2d; + let xs: f32 = size.0 as f32 / 2.; + let ys: f32 = size.1 as f32 / 2.; + let deep: usize = (0.5 / scale) as usize; + + for i in 0..size.0 { + for j in 0..size.1 { + let x: f32 = i as f32; + let y: f32 = j as f32; + + c = f2d::new( + (x - xs) * scale * X_SCAILE + pos.0, + (y - ys) * scale + pos.1, + ); + z = f2d::def(); + for k in 0..deep { + z.mul(z.clone()); + z.add(c.clone()); + if z.len() > 2. { + let mut m = (k as f32) / (deep as f32); + //m = (m / 100.0).round() * 100.0; + m *= 255.0; + colors[1] = Color::Rgb { + r: m as u8, + g: m as u8, + b: m as u8, + }; + execute!(out, SetBackgroundColor(colors[1]), Print(" "), ResetColor,)?; + //print!("#"); + break; + } + } + + if z.len() < 2. { + execute!(out, SetBackgroundColor(colors[0]), Print(" "), ResetColor,)?; + } + } + println!(); + } + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index a7769b4..b8455f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,30 +1,88 @@ -mod my_math; -use my_math::*; +mod console_render; +use std::io::{self, Write}; -fn main(){ - let size: (usize, usize) = (50, 100); - let mut z: f2d; - let mut c: f2d; - //let mut l: f32; +use console_render::*; - for j in 0..size.0{ - for i in 0..size.1{ - c = f2d::new(((i as f32) - (size.1 as f32)/2.)*0.02, ((j as f32) - (size.0 as f32)/2.)*0.04); - z = f2d::def(); - for k in 0..10 { - z.mul(z.clone()); - z.add(c.clone()); +enum InputEvent { + None, + Pos(f32, f32), + Scale(f32), +} + +fn in_pars() -> Result { + let mut input = String::new(); + + print!("In: "); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut input) + .expect("error input scan\n"); + + let worlds: Vec<&str> = input.trim().split(" ").collect(); + if worlds.len() == 0 { + return Err(String::from("zero lenth")); + } + + match worlds[0] { + "pos" => { + //println!("pos!"); + if worlds.len() < 3 { + return Err(String::from("not enough arguments")); } + let r0 = worlds[1].parse::(); + let r1 = worlds[2].parse::(); + if r0.is_err() || r1.is_err() { + return Err(String::from("not enough arguments")); + } + return Ok(InputEvent::Pos(r0.unwrap(), r1.unwrap())); + } + "scl" => { + if worlds.len() < 2 { + return Err(String::from("not enough scale")); + } + let scl = worlds[1].parse::(); + if scl.is_err() { + return Err(String::from("scale is not value")); + } + return Ok(InputEvent::Scale(scl.unwrap())); + } + _ => {} + } - if z.len() < 2. { print!(" "); } - else { print!("#"); } + Ok(InputEvent::None) +} - /* - l = f2d::len(z); - if (l - 20.) < 0.{ print!(" "); } - else { print!("#"); } - */ - } - print!("\n"); +fn main() { + let size: (usize, usize) = (100, 200); + let mut pos: (f32, f32) = (0.0, 0.1); + let mut scale: f32 = 0.01; + let mut render = true; + //m_render(size, 0.007, (-0.4, 0.)); + + //_ = in_pars(); + + while true { + if render { + let _ = m_render(size, scale, pos); + } + let input = in_pars(); + if input.is_err() { + render = false; + println!("ERROR: {}", input.err().unwrap()); + continue; + } else { + match input.unwrap() { + InputEvent::Pos(a, b) => { + pos.0 += a; + pos.1 += b; + } + InputEvent::Scale(s) => { + scale /= s; + } + _ => {} + } + } + + render = true; } } diff --git a/src/my_math/mod.rs b/src/my_math/mod.rs deleted file mode 100644 index 3042a8e..0000000 --- a/src/my_math/mod.rs +++ /dev/null @@ -1,41 +0,0 @@ -pub fn mid(a: f32, b: f32) -> f32{ - (a + b) / 2.0 -} - -#[derive(Clone)] -pub struct f2d{ - x: f32, - y: f32, -} - -impl f2d{ - pub fn new(a: f32, b: f32) -> Self{ - f2d{ - x: a, - y: b, - } - } - pub fn def() -> Self{ - f2d{ - x: 0.0, - y: 0.0, - } - } - // - pub fn len(self) -> f32{ - (self.x * self.x + self.y * self.y).sqrt() - } - - pub fn add(&mut self, b: f2d){ - self.x += b.x; - self.y += b.y; - } - - pub fn mul(&mut self, b: f2d){ - let x: f32 = self.x * b.x - self.y * b.y; - let y: f32 = self.x * b.y + self.y * b.x; - - self.x = x; - self.y = y; - } -}