Compare commits

4 Commits
Author SHA1 Message Date
lemon-cat a4185970d9 lesson 3 2026-05-11 14:29:27 +03:00
lemon-cat 5f038f9bfa home work 2.1 2026-05-07 18:38:01 +03:00
lemon-cat 8b63d4fd7a lesson 2 2026-05-06 17:57:47 +03:00
lemon-cat 9986cbbb28 lesson 1 2026-05-02 18:46:17 +03:00
5 changed files with 300 additions and 1 deletions

No files matched your search

+1
View File
@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024"
[dependencies]
crossterm = "0.29"
+31
View File
@@ -0,0 +1,31 @@
#[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
@@ -0,0 +1,72 @@
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(())
}
+111
View File
@@ -0,0 +1,111 @@
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode},
};
use std::io::{self, Write};
pub enum InputEvent {
None,
Pos(f64, f64),
Scale(f64),
}
pub fn in_pars() -> Result<InputEvent, String> {
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::<f64>();
let r1 = worlds[2].parse::<f64>();
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::<f64>();
if scl.is_err() {
return Err(String::from("scale is not value"));
}
return Ok(InputEvent::Scale(scl.unwrap()));
}
_ => {}
}
Ok(InputEvent::None)
}
pub enum KeyEvent {
None,
ESC,
R,
A,
W,
S,
D,
Q,
E,
}
pub fn key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
_ = enable_raw_mode();
if let Event::Key(key) = event::read()? {
match key.kind {
KeyEventKind::Repeat => {
//println!("Key is Repeat! {}", key.code);
}
KeyEventKind::Press => {
//println!("Key is press! {}", key.code);
match key.code {
KeyCode::Esc => {
_ = disable_raw_mode();
return Ok(Some(KeyEvent::ESC));
}
KeyCode::Char('a') => {
return Ok(Some(KeyEvent::A));
}
KeyCode::Char('w') => {
return Ok(Some(KeyEvent::W));
}
KeyCode::Char('s') => {
return Ok(Some(KeyEvent::S));
}
KeyCode::Char('d') => {
return Ok(Some(KeyEvent::D));
}
KeyCode::Char('q') => {
return Ok(Some(KeyEvent::Q));
}
KeyCode::Char('e') => {
return Ok(Some(KeyEvent::E));
}
_ => {}
}
}
KeyEventKind::Release => {
//println!("Key is replease! {}", key.code);
}
_ => {}
}
}
//_ = disable_raw_mode();
Ok(None)
}
+85 -1
View File
@@ -1,3 +1,87 @@
mod console_render;
use std::arch::x86_64::_SIDD_MASKED_NEGATIVE_POLARITY;
use console_render::*;
mod input;
use input::*;
enum Input_type {
console,
keyborde,
}
fn main() {
println!("Hello, world!");
let size: (usize, usize) = (100, 400);
let mut pos: (f64, f64) = (0.0, 0.0);
let mut scale: f64 = 0.01;
let mut render = true;
let mut it = Input_type::keyborde;
while true {
if render {
let _ = m_render(size, scale, pos);
}
match it {
Input_type::console => {
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 * scale * 10.0;
pos.1 += b * scale * 10.0;
}
InputEvent::Scale(s) => {
if s == 0.0 {
println!("Scale is 0 !!!");
} else {
scale /= s;
}
}
_ => {}
}
}
}
Input_type::keyborde => {
let input = key_pars();
if input.is_err() {
render = false;
println!("ERROR: {}", input.err().unwrap());
continue;
} else {
if let Some(k) = input.unwrap() {
match k {
KeyEvent::ESC => {
it = Input_type::console;
}
KeyEvent::A => {
pos.1 -= scale as f64 * 10.0;
}
KeyEvent::W => {
pos.0 -= scale as f64 * 10.0;
}
KeyEvent::S => {
pos.0 += scale as f64 * 10.0;
}
KeyEvent::D => {
pos.1 += scale as f64 * 10.0;
}
KeyEvent::Q => {
scale /= 1.1;
}
KeyEvent::E => {
scale *= 1.1;
}
_ => {}
}
}
}
}
_ => {}
}
}
}