From b57ed1fa706361b3c35360b025a2c875623c333e Mon Sep 17 00:00:00 2001 From: Yandrik Date: Mon, 2 Jan 2023 13:21:56 +0100 Subject: [PATCH] feat: added zoom and colors --- src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9b5fa13..6c0e6ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use crossterm::cursor::MoveTo; use crossterm::style::Attribute::Dim; -use crossterm::style::Print; +use crossterm::style::{Color, Print, SetForegroundColor}; use crossterm::terminal::{Clear, ClearType, enable_raw_mode}; use crossterm::{execute, QueueableCommand}; use ndarray::{arr1, arr2, Array1, Array2}; @@ -22,6 +22,8 @@ enum Dimension { struct Mesh { points: Vec>, edges: Vec<[usize; 2]>, + line_color: Option, + node_color: Option, } #[inline] @@ -160,6 +162,7 @@ static ANGLE: Mutex = Mutex::new(0.); fn draw_rotating_mesh(meshes: Vec) -> anyhow::Result<()> { let mut angle = (0., 0., 0.); + let mut zoom = 1.; loop { @@ -172,6 +175,8 @@ fn draw_rotating_mesh(meshes: Vec) -> anyhow::Result<()> { KeyCode::Char('d') => angle.2 -= 6., KeyCode::Char('q') => angle.1 += 6., KeyCode::Char('e') => angle.1 -= 6., + KeyCode::Char(',') => zoom = 0.2f32.max(zoom - 0.02), + KeyCode::Char('.') => zoom = 1f32.min(zoom + 0.02), KeyCode::Esc | KeyCode::Backspace | KeyCode::Char('c') => exit(0), _ => {}, } @@ -185,6 +190,7 @@ fn draw_rotating_mesh(meshes: Vec) -> anyhow::Result<()> { let x_matrix = rotation_matrix(angle.0, Dimension::X); let y_matrix = rotation_matrix(angle.1, Dimension::Y); let z_matrix = rotation_matrix(angle.2, Dimension::Z); + let zoom_matrix = scale(zoom, zoom, zoom); let cam_matrix = ortho_matrix(); let mut stdout = stdout(); @@ -200,10 +206,14 @@ fn draw_rotating_mesh(meshes: Vec) -> anyhow::Result<()> { .map(|pt| x_matrix.dot(&pt)) .map(|pt| y_matrix.dot(&pt)) .map(|pt| z_matrix.dot(&pt)) + .map(|pt| zoom_matrix.dot(&pt)) .map(|pt| cam_matrix.dot(&pt)) .map(|pt| pt + arr1(&[30., 30.])) // draw shift .collect::>>(); + // set edge color before edge drawing + stdout.queue(SetForegroundColor(mesh.line_color.unwrap_or(Color::White)))?; + for edge in edges.iter() { let origin = &projected_points[edge[0]]; let dest = &projected_points[edge[1]]; @@ -217,6 +227,9 @@ fn draw_rotating_mesh(meshes: Vec) -> anyhow::Result<()> { )?; } + // set mesh point color + stdout.queue(SetForegroundColor(mesh.node_color.unwrap_or(Color::White)))?; + for pt in &projected_points { let pt = pt; stdout @@ -236,6 +249,26 @@ fn main() -> anyhow::Result<()> { enable_raw_mode()?; + let random_rectangle = Mesh { + points: vec![ + arr1(&[-10., 0., 0.]), + arr1(&[0., 10., 0.]), + arr1(&[10., 0., 0.]), + arr1(&[0., -10., 0.]), + + + ], + edges: vec![ + [0, 1], + [1, 2], + [2, 3], + [3, 0], + ], + line_color: Some(Color::Red), + node_color: Some(Color::DarkRed) + }; + + let cube = vec![ arr1(&[-10., -10., 10.]), arr1(&[-10., 10., 10.]), @@ -294,14 +327,19 @@ fn main() -> anyhow::Result<()> { [2, 4], [2, 5], ], + line_color: Some(Color::Blue), + node_color: Some(Color::White), }; draw_rotating_mesh(vec![ + random_rectangle, + thing, Mesh { points: cube, edges, + line_color: Some(Color::Green), + node_color: Some(Color::White), }, - thing, ])?; unreachable!()