input pars, colored render
This commit is contained in:
1 parent
0a24863b34
commit
e122bd0a70
4 files changed
+170
-36
No files matched your search
@@ -3,4 +3,6 @@ name = "education-rs-rep"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
|
||||
[dependencies]
|
||||
crossterm = "0.29"
|
||||
+90
-24
@@ -1,36 +1,102 @@
|
||||
use std::io::{self, Write};
|
||||
|
||||
mod my_match;
|
||||
use my_match::*;
|
||||
mod render;
|
||||
use render::*;
|
||||
|
||||
enum InputEvent {
|
||||
None,
|
||||
Pos(f32, f32),
|
||||
Scale(f32),
|
||||
}
|
||||
|
||||
fn intput_pars() -> Result<InputEvent, String>{
|
||||
let mut input = String::new();
|
||||
|
||||
print!("in: ");
|
||||
io::stdout().flush().unwrap();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("erore input!!!!!");
|
||||
|
||||
let words: Vec<&str> = input
|
||||
.trim()
|
||||
.split(" ")
|
||||
.collect();
|
||||
|
||||
if words.len() == 0 {
|
||||
println!("nol");
|
||||
return Err(String::from("zero len"));
|
||||
}
|
||||
|
||||
println!("n: {:?}", words);
|
||||
|
||||
match words[0] {
|
||||
"pos" => {
|
||||
if words.len() < 3 {
|
||||
return Err(String::from("not enough arguments"));
|
||||
}
|
||||
|
||||
let r0 = words[1].parse::<f32>();
|
||||
let r1 = words[2].parse::<f32>();
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
let r0 = words[1].parse::<f32>();
|
||||
|
||||
if r0.is_err(){
|
||||
return Err(String::from("not a namber"));
|
||||
}
|
||||
|
||||
return Ok(InputEvent::Scale(r0.unwrap()));
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
Ok(InputEvent::None)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let size: (usize, usize) = (240, 80);
|
||||
let mut s = 40.0;
|
||||
let mut pos: (f32, f32) = (-0.5, 0.0);
|
||||
|
||||
let mut z: f2d;
|
||||
let mut c: f2d;
|
||||
let s = 40.0;
|
||||
let sx = 1.8;
|
||||
|
||||
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 / (s * sx), uv.1 / s);
|
||||
let mut render = true;
|
||||
|
||||
c = f2d::new(uv.0, uv.1);
|
||||
z = f2d::def();
|
||||
|
||||
for _ in 0..100 {
|
||||
z.mul(z.clone());
|
||||
z.add(c.clone());
|
||||
}
|
||||
|
||||
if z.len() < 2. {
|
||||
print!("$");
|
||||
} else {
|
||||
print!(" ");
|
||||
while true {
|
||||
if render {
|
||||
let _ = m_render(size, s, pos);
|
||||
}
|
||||
let inp = intput_pars();
|
||||
if inp.is_err() {
|
||||
render = false;
|
||||
println!("ERROR: {}", inp.err().unwrap());
|
||||
continue;
|
||||
} else {
|
||||
match inp.unwrap() {
|
||||
InputEvent::Pos(a, b) => {
|
||||
pos.0 += a * (1.0 / s);
|
||||
pos.1 += b * (1.0 / s);
|
||||
},
|
||||
InputEvent::Scale(sk) => {
|
||||
println!("ssss");
|
||||
s *= sk;
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
print!("\n");
|
||||
|
||||
|
||||
render = true;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,3 @@
|
||||
pub fn mid(a: f32, b: f32) -> f32{
|
||||
return (a+b)/2.;
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct f2d{
|
||||
x: f32,
|
||||
@@ -10,10 +5,10 @@ pub struct f2d{
|
||||
}
|
||||
|
||||
impl f2d{
|
||||
pub fn new(x: f32, y: f32) -> Self{
|
||||
pub fn new(a: (f32, f32)) -> Self{
|
||||
f2d {
|
||||
x: x,
|
||||
y: y,
|
||||
x: a.0,
|
||||
y: a.1,
|
||||
}
|
||||
}
|
||||
pub fn def() -> Self{
|
||||
@@ -23,10 +18,7 @@ impl f2d{
|
||||
}
|
||||
}
|
||||
// вычесления
|
||||
pub fn len(self) -> f32{
|
||||
/*if self.x == 0. && self.y == 0.{
|
||||
return 0.;
|
||||
}*/
|
||||
pub fn len(&self) -> f32{
|
||||
(self.x * self.x + self.y * self.y).sqrt()
|
||||
}
|
||||
pub fn add(&mut self, b: f2d) {
|
||||
@@ -0,0 +1,74 @@
|
||||
use crossterm::{
|
||||
execute,
|
||||
style::{Color, Print, SetBackgroundColor, ResetColor},
|
||||
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 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;
|
||||
|
||||
let mut out = stdout();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user