Compare commits

3 Commits
Author SHA1 Message Date
vova-rs 40d1a1468b key bord navigation 2026-05-11 13:29:44 +03:00
vova-rs e122bd0a70 input pars, colored render 2026-05-06 17:58:53 +03:00
vova-rs 0a24863b34 mandelbrot 2026-05-02 18:46:01 +03:00
7 changed files with 253 additions and 217 deletions

No files matched your search

+1
View File
@@ -3,5 +3,6 @@ name = "education-rs-rep"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
crossterm = "0.29" crossterm = "0.29"
-31
View File
@@ -1,31 +0,0 @@
#[derive(Clone)]
pub struct f2d {
x: f64,
y: f64,
}
impl f2d {
pub fn new(a: f64, b: f64) -> Self {
f2d { x: a, y: b }
}
pub fn def() -> Self {
f2d { x: 0.0, y: 0.0 }
}
//
pub fn len(&self) -> f64 {
self.x * self.x + self.y * self.y
}
pub fn add(&mut self, b: f2d) {
self.x += b.x;
self.y += b.y;
}
pub fn mul(&mut self, b: f2d) {
let x: f64 = self.x * b.x - self.y * b.y;
let y: f64 = self.x * b.y + self.y * b.x;
self.x = x;
self.y = y;
}
}
-72
View File
@@ -1,72 +0,0 @@
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(())
}
+74 -62
View File
@@ -2,110 +2,122 @@ use crossterm::{
event::{self, Event, KeyCode, KeyEventKind}, event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode}, terminal::{disable_raw_mode, enable_raw_mode},
}; };
use std::time::Duration;
use std::io::{self, Write}; use std::io::{self, Write};
pub enum InputEvent { pub enum InputEvent {
None, None,
Pos(f64, f64), Pos(f32, f32),
Scale(f64), Scale(f32),
} }
pub fn in_pars() -> Result<InputEvent, String> { pub fn intput_pars() -> Result<InputEvent, String>{
let mut input = String::new(); let mut input = String::new();
print!("In: "); print!("in: ");
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
io::stdin() io::stdin()
.read_line(&mut input) .read_line(&mut input)
.expect("error input scan\n"); .expect("erore input!!!!!");
let worlds: Vec<&str> = input.trim().split(" ").collect(); let words: Vec<&str> = input
if worlds.len() == 0 { .trim()
return Err(String::from("zero lenth")); .split(" ")
.collect();
if words.len() == 0 {
println!("nol");
return Err(String::from("zero len"));
} }
match worlds[0] { println!("n: {:?}", words);
match words[0] {
"pos" => { "pos" => {
//println!("pos!"); if words.len() < 3 {
if worlds.len() < 3 {
return Err(String::from("not enough arguments")); return Err(String::from("not enough arguments"));
} }
let r0 = worlds[1].parse::<f64>();
let r1 = worlds[2].parse::<f64>(); let r0 = words[1].parse::<f32>();
let r1 = words[2].parse::<f32>();
if r0.is_err() || r1.is_err() { if r0.is_err() || r1.is_err() {
return Err(String::from("not a namber"));
}
return Ok(InputEvent::Pos(r0.unwrap(), r1.unwrap()));
},
"scl" => {
if words.len() < 2 {
return Err(String::from("not enough arguments")); return Err(String::from("not enough arguments"));
} }
return Ok(InputEvent::Pos(r0.unwrap(), r1.unwrap()));
let r0 = words[1].parse::<f32>();
if r0.is_err(){
return Err(String::from("not a namber"));
} }
"scl" => {
if worlds.len() < 2 { return Ok(InputEvent::Scale(r0.unwrap()));
return Err(String::from("not enough scale")); },
} _ => {},
let scl = worlds[1].parse::<f64>();
if scl.is_err() {
return Err(String::from("scale is not value"));
}
return Ok(InputEvent::Scale(scl.unwrap()));
}
_ => {}
} }
Ok(InputEvent::None) Ok(InputEvent::None)
} }
/////////////////////////////
/////////////////////////////
///////////////////////////
pub enum KeyEvent { pub enum KeyEvent {
None, C,
ESC, // R,
R, XSh(i8),
A, YSh(i8),
W, Scl(bool),
S,
D,
Q,
E,
} }
pub fn key_pars() -> Result<Option<KeyEvent>, std::io::Error> { pub fn key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
_ = enable_raw_mode(); enable_raw_mode()?;
// if event::poll(Duration::from_millis(0))? {
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
match key.kind { match key.kind {
KeyEventKind::Repeat => {
//println!("Key is Repeat! {}", key.code);
}
KeyEventKind::Press => { KeyEventKind::Press => {
//println!("Key is press! {}", key.code); if key.code == KeyCode::Char('c'){
match key.code { return Ok(Some(KeyEvent::C));
KeyCode::Esc => {
_ = disable_raw_mode();
return Ok(Some(KeyEvent::ESC));
} }
KeyCode::Char('a') => {
return Ok(Some(KeyEvent::A)); if key.code == KeyCode::Up{
return Ok(Some(KeyEvent::XSh(1)));
} }
KeyCode::Char('w') => { if key.code == KeyCode::Down{
return Ok(Some(KeyEvent::W)); return Ok(Some(KeyEvent::XSh(-1)));
} }
KeyCode::Char('s') => { if key.code == KeyCode::Left{
return Ok(Some(KeyEvent::S)); return Ok(Some(KeyEvent::YSh(1)));
} }
KeyCode::Char('d') => { if key.code == KeyCode::Right{
return Ok(Some(KeyEvent::D)); return Ok(Some(KeyEvent::YSh(-1)));
} }
KeyCode::Char('q') => {
return Ok(Some(KeyEvent::Q)); if key.code == KeyCode::Char('-'){
return Ok(Some(KeyEvent::Scl(false)));
}
if key.code == KeyCode::Char('='){
return Ok(Some(KeyEvent::Scl(true)));
} }
KeyCode::Char('e') => {
return Ok(Some(KeyEvent::E));
} }
_ => {} _ => {}
} }
} }
KeyEventKind::Release => {
//println!("Key is replease! {}", key.code); disable_raw_mode()?;
}
_ => {}
}
}
//_ = disable_raw_mode();
Ok(None) Ok(None)
} }
+61 -52
View File
@@ -1,87 +1,96 @@
mod console_render; use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode},
};
use std::arch::x86_64::_SIDD_MASKED_NEGATIVE_POLARITY; mod render;
use render::*;
use console_render::*;
mod input; mod input;
use input::*; use input::*;
enum Input_type { enum InputType{
console, Console,
keyborde, Keybord,
} }
fn main() {
let size: (usize, usize) = (100, 400);
let mut pos: (f64, f64) = (0.0, 0.0); fn main() -> std::io::Result<()> {
let mut scale: f64 = 0.01; let size: (usize, usize) = (360, 100);
let mut render = true; let mut s = 40.0;
let mut it = Input_type::keyborde; let mut pos: (f32, f32) = (-0.5, 0.0);
let mut it = InputType::Keybord;
let mut render = false;
while true { while true {
if render { if render {
let _ = m_render(size, scale, pos); let _ = m_render(size, s, pos);
} }
match it { match it {
Input_type::console => { InputType::Console => {
let input = in_pars(); let inp = intput_pars();
if input.is_err() { if inp.is_err() {
render = false; render = false;
println!("ERROR: {}", input.err().unwrap()); println!("ERROR: {}", inp.err().unwrap());
continue; continue;
} else { } else {
match input.unwrap() { match inp.unwrap() { /*Ok(InputEvent) >> InputEvent*/
InputEvent::Pos(a, b) => { InputEvent::Pos(a, b) => {
pos.0 += a * scale * 10.0; pos.0 += a * (1.0 / s);
pos.1 += b * scale * 10.0; pos.1 += b * (1.0 / s);
},
InputEvent::Scale(sk) => {
println!("ssss");
s *= sk;
} }
InputEvent::Scale(s) => { _ => {},
if s == 0.0 {
println!("Scale is 0 !!!");
} else {
scale /= s;
} }
} }
_ => {} render = true;
} }
} InputType::Keybord => {
} let inp = key_pars();
Input_type::keyborde => { if inp.is_err(){
let input = key_pars();
if input.is_err() {
render = false; render = false;
println!("ERROR: {}", input.err().unwrap()); println!("ERROR: {}", inp.err().unwrap());
continue; continue;
} else { } else {
if let Some(k) = input.unwrap() { if let Some(k) = inp.unwrap(){
match k { match k {
KeyEvent::ESC => { KeyEvent::C => {
it = Input_type::console; it = InputType::Console;
// render = true;
disable_raw_mode()?;
} }
KeyEvent::A => { KeyEvent::XSh(a) => {
pos.1 -= scale as f64 * 10.0; pos.1 += 1.0 / s * a as f32;
render = true;
continue;
} }
KeyEvent::W => { KeyEvent::YSh(a) => {
pos.0 -= scale as f64 * 10.0; pos.0 += 1.0 / s * a as f32;
render = true;
continue;
} }
KeyEvent::S => { KeyEvent::Scl(a) => {
pos.0 += scale as f64 * 10.0; if a {
s *= 1.1;
} else {
s /= 1.1;
} }
KeyEvent::D => { render = true;
pos.1 += scale as f64 * 10.0; continue;
}
KeyEvent::Q => {
scale /= 1.1;
}
KeyEvent::E => {
scale *= 1.1;
} }
_ => {} _ => {}
} }
} }
} }
} render = false;
_ => {}
} }
} }
} }
Ok(())
}
+35
View File
@@ -0,0 +1,35 @@
#[derive(Clone)]
pub struct f2d{
x: f32,
y: f32,
}
impl f2d{
pub fn new(a: (f32, f32)) -> Self{
f2d {
x: a.0,
y: a.1,
}
}
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;
}
}
+82
View File
@@ -0,0 +1,82 @@
use crossterm::{
execute,
queue,
style::{Color, Print, SetBackgroundColor, ResetColor},
// terminal::{Clear, ClearType},
cursor::MoveTo,
};
use std::io::stdout;
mod c_math;
use c_math::*;
const X_SCALE: f32 = 1.8;
pub fn m_render(
size: (usize, usize),
scale: f32,
pos: (f32, f32)
) -> Result<(), Box<dyn std::error::Error>> {
let mut out = stdout();
execute!(
out,
MoveTo(0, 0),
)?;
let colors = [
Color::Rgb { r: 49, g: 64, b: 47},
Color::Rgb { r: 93, g: 141, b: 93},
Color::Rgb { r: 163, g: 208, b: 181},
Color::Rgb { r: 180, g: 214, b: 173},
Color::Rgb { r: 114, g: 167, b: 91},
];
let mut z: f2d;
let mut c: f2d;
for j in 0..size.1 {
for i in 0..size.0 {
let mut uv: (f32, f32) = (i as f32, j as f32);
uv = (uv.0 - size.0 as f32 / 2., uv.1 - size.1 as f32 / 2.);
uv = (uv.0 / (scale * X_SCALE) + pos.0, uv.1 / scale + pos.1);
c = f2d::new(uv);
z = f2d::def();
let mut n = -1;
for k in 0..100 {
z.mul(z.clone());
z.add(c.clone());
if z.len() > 2.0 {
n = k as i32;
break;
}
}
if n == -1{
execute!(
out,
SetBackgroundColor(Color::Rgb { r: 0, g: 0, b: 0}),
Print(" "),
ResetColor,
)?;
} else {
let index = (n as usize / 2) % colors.len();
execute!(
out,
SetBackgroundColor(colors[index]),
Print(" "),
ResetColor,
)?;
// print!("{}|", n);
}
}
queue!(out, Print("\r\n"))?;
}
Ok(())
}