lesson 3
This commit is contained in:
1 parent
5f038f9bfa
commit
a4185970d9
4 files changed
+214
-158
No files matched your search
@@ -1,23 +1,19 @@
|
|||||||
pub fn mid(a: f32, b: f32) -> f32 {
|
|
||||||
(a + b) / 2.0
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct f2d {
|
pub struct f2d {
|
||||||
x: f32,
|
x: f64,
|
||||||
y: f32,
|
y: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl f2d {
|
impl f2d {
|
||||||
pub fn new(a: f32, b: f32) -> Self {
|
pub fn new(a: f64, b: f64) -> Self {
|
||||||
f2d { x: a, y: b }
|
f2d { x: a, y: b }
|
||||||
}
|
}
|
||||||
pub fn def() -> Self {
|
pub fn def() -> Self {
|
||||||
f2d { x: 0.0, y: 0.0 }
|
f2d { x: 0.0, y: 0.0 }
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
pub fn len(&self) -> f32 {
|
pub fn len(&self) -> f64 {
|
||||||
(self.x * self.x + self.y * self.y).sqrt()
|
self.x * self.x + self.y * self.y
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, b: f2d) {
|
pub fn add(&mut self, b: f2d) {
|
||||||
@@ -26,8 +22,8 @@ impl f2d {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn mul(&mut self, b: f2d) {
|
pub fn mul(&mut self, b: f2d) {
|
||||||
let x: f32 = self.x * b.x - self.y * b.y;
|
let x: f64 = self.x * b.x - self.y * b.y;
|
||||||
let y: f32 = self.x * b.y + self.y * b.x;
|
let y: f64 = self.x * b.y + self.y * b.x;
|
||||||
|
|
||||||
self.x = x;
|
self.x = x;
|
||||||
self.y = y;
|
self.y = y;
|
||||||
|
|||||||
+27
-73
@@ -1,118 +1,72 @@
|
|||||||
use crossterm::{
|
use crossterm::{
|
||||||
//cursor::MoveTo,
|
//cursor::MoveTo,
|
||||||
|
cursor::MoveTo,
|
||||||
execute,
|
execute,
|
||||||
|
queue,
|
||||||
style::{Color, Print, ResetColor, SetBackgroundColor},
|
style::{Color, Print, ResetColor, SetBackgroundColor},
|
||||||
|
terminal::{Clear, ClearType},
|
||||||
};
|
};
|
||||||
use std::io::stdout;
|
use std::io::{Write, stdout};
|
||||||
|
|
||||||
mod c_math;
|
mod c_math;
|
||||||
use c_math::*;
|
use c_math::*;
|
||||||
|
|
||||||
const X_SCAILE: f32 = 2.7;
|
const X_SCAILE: f64 = 2.7;
|
||||||
pub fn m_render(
|
pub fn m_render(
|
||||||
size: (usize, usize),
|
size: (usize, usize),
|
||||||
scale: f32,
|
scale: f64,
|
||||||
pos: (f32, f32),
|
pos: (f64, f64),
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/*
|
|
||||||
let mut colors = [
|
|
||||||
Color::Rgb { r: 0, g: 0, b: 0 },
|
|
||||||
Color::Rgb {
|
|
||||||
r: 150,
|
|
||||||
g: 90,
|
|
||||||
b: 150,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
*/
|
|
||||||
|
|
||||||
let mut grad = Color::Rgb { r: 0, g: 0, b: 0 };
|
let mut grad = Color::Rgb { r: 0, g: 0, b: 0 };
|
||||||
let mut out = stdout();
|
let mut out = stdout();
|
||||||
|
queue!(out, MoveTo(0, 0))?;
|
||||||
|
//out.flush()?;
|
||||||
|
|
||||||
let mut z: f2d;
|
let mut z: f2d;
|
||||||
let mut c: f2d;
|
let mut c: f2d;
|
||||||
let xs: f32 = size.0 as f32 / 2.;
|
let xs: f64 = size.0 as f64 / 2.;
|
||||||
let ys: f32 = size.1 as f32 / 2.;
|
let ys: f64 = size.1 as f64 / 2.;
|
||||||
let deep: f32 = 0.2 / scale;
|
//let deep: f32 = 1.0 / scale;
|
||||||
|
let deep = (100.0 + scale.log10().abs() * 80.0) as usize;
|
||||||
|
|
||||||
for i in 0..size.0 {
|
for i in 0..size.0 {
|
||||||
for j in 0..size.1 {
|
for j in 0..size.1 {
|
||||||
let x: f32 = i as f32;
|
let x: f64 = i as f64;
|
||||||
let y: f32 = j as f32;
|
let y: f64 = j as f64;
|
||||||
|
|
||||||
c = f2d::new(
|
c = f2d::new(
|
||||||
(x - xs) * scale * X_SCAILE + pos.0,
|
(x - xs) * scale as f64 * X_SCAILE + pos.0,
|
||||||
(y - ys) * scale + pos.1,
|
(y - ys) * scale as f64 + pos.1,
|
||||||
);
|
);
|
||||||
z = f2d::def();
|
z = f2d::def();
|
||||||
let mut m: f32 = 0.0;
|
let mut m: usize = 0;
|
||||||
let mut k = true;
|
let mut k = true;
|
||||||
|
|
||||||
while k {
|
while k {
|
||||||
z.mul(z.clone());
|
z.mul(z.clone());
|
||||||
z.add(c.clone());
|
z.add(c.clone());
|
||||||
m += 1.0;
|
m += 1;
|
||||||
k = (z.len() < 2.0) && (m < deep);
|
k = (z.len() < 4.0) && (m < deep);
|
||||||
//if m > deep {
|
|
||||||
// println!("Stop me!!!");
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if z.len() > 2.0 {
|
if z.len() > 2.0 {
|
||||||
//m /= deep;
|
let smooth = (m as f64 + 1.0 - z.len().ln().ln() / 2.0_f64.ln()) as f32;
|
||||||
let smooth = m + 1.0 - z.len().ln().ln() / 2.0_f32.ln();
|
|
||||||
/*
|
|
||||||
let t = (smooth / deep).clamp(0.0, 1.0);
|
|
||||||
grad = Color::Rgb {
|
|
||||||
r: (t * 230.0 + 10.0) as u8,
|
|
||||||
g: (t * 100.0 + 10.0) as u8,
|
|
||||||
b: (t * 240.0 + 10.0) as u8,
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
grad = Color::Rgb {
|
grad = Color::Rgb {
|
||||||
r: (((smooth * 0.1).sin() + 1.0) * 230.0) as u8,
|
r: (((smooth * 0.1).sin() + 1.0) * 230.0) as u8,
|
||||||
g: (((smooth * 0.1).sin() + 1.0) * 100.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,
|
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: (m * 230.0 + 10.0) as u8,
|
|
||||||
g: (m * 100.0 + 10.0) as u8,
|
|
||||||
b: (m * 230.0 + 10.0) as u8,
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
execute!(out, SetBackgroundColor(grad), Print(" "), ResetColor)?;
|
|
||||||
grad = Color::Rgb { r: 0, g: 0, b: 0 };
|
grad = Color::Rgb { r: 0, g: 0, b: 0 };
|
||||||
} else {
|
} else {
|
||||||
execute!(out, SetBackgroundColor(grad), Print(" "), ResetColor,)?;
|
queue!(out, SetBackgroundColor(grad), Print(" "), ResetColor,)?;
|
||||||
|
out.flush()?;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
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!();
|
queue!(out, Print("\r\n"))?;
|
||||||
}
|
}
|
||||||
|
out.flush()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
+69
-74
@@ -1,92 +1,87 @@
|
|||||||
mod console_render;
|
mod console_render;
|
||||||
use std::io::{self, Write};
|
|
||||||
|
use std::arch::x86_64::_SIDD_MASKED_NEGATIVE_POLARITY;
|
||||||
|
|
||||||
use console_render::*;
|
use console_render::*;
|
||||||
|
|
||||||
enum InputEvent {
|
mod input;
|
||||||
None,
|
use input::*;
|
||||||
Pos(f32, f32),
|
|
||||||
Scale(f32),
|
enum Input_type {
|
||||||
|
console,
|
||||||
|
keyborde,
|
||||||
}
|
}
|
||||||
|
|
||||||
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::<f32>();
|
|
||||||
let r1 = worlds[2].parse::<f32>();
|
|
||||||
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::<f32>();
|
|
||||||
if scl.is_err() {
|
|
||||||
return Err(String::from("scale is not value"));
|
|
||||||
}
|
|
||||||
return Ok(InputEvent::Scale(scl.unwrap()));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(InputEvent::None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let size: (usize, usize) = (200, 450);
|
let size: (usize, usize) = (100, 400);
|
||||||
let mut pos: (f32, f32) = (0.0, 0.0);
|
let mut pos: (f64, f64) = (0.0, 0.0);
|
||||||
let mut scale: f32 = 0.01;
|
let mut scale: f64 = 0.01;
|
||||||
let mut render = true;
|
let mut render = true;
|
||||||
//m_render(size, 0.007, (-0.4, 0.));
|
let mut it = Input_type::keyborde;
|
||||||
|
|
||||||
//_ = in_pars();
|
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
if render {
|
if render {
|
||||||
let _ = m_render(size, scale, pos);
|
let _ = m_render(size, scale, pos);
|
||||||
}
|
}
|
||||||
let input = in_pars();
|
match it {
|
||||||
if input.is_err() {
|
Input_type::console => {
|
||||||
render = false;
|
let input = in_pars();
|
||||||
println!("ERROR: {}", input.err().unwrap());
|
if input.is_err() {
|
||||||
continue;
|
render = false;
|
||||||
} else {
|
println!("ERROR: {}", input.err().unwrap());
|
||||||
match input.unwrap() {
|
continue;
|
||||||
InputEvent::Pos(a, b) => {
|
} else {
|
||||||
pos.0 += a * scale * 10.0;
|
match input.unwrap() {
|
||||||
pos.1 += b * scale * 10.0;
|
InputEvent::Pos(a, b) => {
|
||||||
}
|
pos.0 += a * scale * 10.0;
|
||||||
InputEvent::Scale(s) => {
|
pos.1 += b * scale * 10.0;
|
||||||
if s == 0.0 {
|
}
|
||||||
println!("Scale is 0 !!!");
|
InputEvent::Scale(s) => {
|
||||||
} else {
|
if s == 0.0 {
|
||||||
scale /= s;
|
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;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
render = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user