lesson 2
This commit is contained in:
1 parent
9986cbbb28
commit
8b63d4fd7a
6 files changed
+186
-65
No files matched your search
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
crossterm = "0.29"
|
||||||
Whitespace-only changes.
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<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 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(())
|
||||||
|
}
|
||||||
+82
-24
@@ -1,30 +1,88 @@
|
|||||||
mod my_math;
|
mod console_render;
|
||||||
use my_math::*;
|
use std::io::{self, Write};
|
||||||
|
|
||||||
fn main(){
|
use console_render::*;
|
||||||
let size: (usize, usize) = (50, 100);
|
|
||||||
let mut z: f2d;
|
|
||||||
let mut c: f2d;
|
|
||||||
//let mut l: f32;
|
|
||||||
|
|
||||||
for j in 0..size.0{
|
enum InputEvent {
|
||||||
for i in 0..size.1{
|
None,
|
||||||
c = f2d::new(((i as f32) - (size.1 as f32)/2.)*0.02, ((j as f32) - (size.0 as f32)/2.)*0.04);
|
Pos(f32, f32),
|
||||||
z = f2d::def();
|
Scale(f32),
|
||||||
for k in 0..10 {
|
}
|
||||||
z.mul(z.clone());
|
|
||||||
z.add(c.clone());
|
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>();
|
||||||
if z.len() < 2. { print!(" "); }
|
let r1 = worlds[2].parse::<f32>();
|
||||||
else { print!("#"); }
|
if r0.is_err() || r1.is_err() {
|
||||||
|
return Err(String::from("not enough arguments"));
|
||||||
/*
|
}
|
||||||
l = f2d::len(z);
|
return Ok(InputEvent::Pos(r0.unwrap(), r1.unwrap()));
|
||||||
if (l - 20.) < 0.{ print!(" "); }
|
|
||||||
else { print!("#"); }
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
print!("\n");
|
"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() {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user