From f5cc41c1328a034bbdee61f2768e0ef82f36fbf8 Mon Sep 17 00:00:00 2001 From: FantasyPvP <80643031+FantasyPvP@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:26:47 +0100 Subject: [PATCH] updated grapher fixed issue where grapher would fail with functions due to some negative values being undefined --- src/user/bin/grapher.rs | 69 ++++++++++++++++++++++++++++------------- src/user/bin/shell.rs | 7 ++++- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/user/bin/grapher.rs b/src/user/bin/grapher.rs index 9639ad6..60f717a 100644 --- a/src/user/bin/grapher.rs +++ b/src/user/bin/grapher.rs @@ -1,5 +1,5 @@ use alloc::string::{String, ToString}; -use alloc::vec; +use alloc::{format, vec}; use alloc::vec::Vec; use alloc::boxed::Box; use async_trait::async_trait; @@ -9,41 +9,56 @@ use crate::std::application::{Application, Error}; use crate::std::frame::Element; use crate::std::io::{Screen, Stdin}; +const OFFSET_X: i64 = 40; +const OFFSET_Y: i64 = 12; + pub struct Grapher { - points: Vec, + points: Vec, + frame: Vec>, } -struct Point { - x: i16, - y: i16, +struct PointF64 { + x: f64, + y: f64, } +struct PointI64 { + x: i64, + y: i64, +} + #[async_trait] impl Application for Grapher { fn new() -> Self { Self { points: Vec::new(), + frame: vec![vec![' '; 80]; 25], } } async fn run(&mut self, args: Vec) -> Result<(), Error> { let mut equation: String = args.into_iter().collect(); use super::calc; - for x in -40..40 { + for x in -4000..4000 { + let x = x as f64 / 100.0; + let new_eq = equation.chars().map(|c| { - if c == 'x' { x.to_string() } else { c.to_string() } + if c == 'x' { format!("({})", x) } else { c.to_string() } }).collect::(); - let y = calc::calc_outer(new_eq).map_err(|_| Error::ApplicationError(String::from("failed to calculate")))?; + let fx = calc::calc_outer(new_eq).map_err(|_| Error::ApplicationError(String::from("failed to calculate"))); + + if let Ok(y) = fx { + self.render_point(PointF64 { + x, + y, + }) + } - self.points.push(Point { - x, - y: y as i16, - }) }; Screen::application_mode(); - self.render(); + self.display(); loop { match Stdin::keystroke().await { @@ -58,17 +73,27 @@ impl Application for Grapher { } impl Grapher { - fn render(&self) { - let mut frame: Vec> = vec![vec![' '; 80]; 25]; - self.points.iter().filter(|i| i.x >= -40 && i.x < 40 && i.y >= -12 && i.y <= 12).for_each(|i| { - let offset_x = i.x + 40; - let offset_y = i.y + 12; - //println!("{} {}", i.x, i.y); - frame[24-offset_y as usize][offset_x as usize] = '*'; - }); + fn render_point(&mut self, point: PointF64) { - let mut elem = Element::generate(frame, (80, 25)); + let point = PointI64 { + x: point.x as i64, + y: point.y as i64, + }; + + if point.x < -40 || point.x >= 40 || point.y < -12 || point.y >= 12 { + return; + } + + let offset_x = point.x + OFFSET_X; + let offset_y = point.y + OFFSET_Y; + + self.frame[24-offset_y as usize][offset_x as usize] = '*'; + } + + + fn display(&mut self) { + let mut elem = Element::generate(self.frame.clone(), (80, 25)); elem.render((0, 0)); } } \ No newline at end of file diff --git a/src/user/bin/shell.rs b/src/user/bin/shell.rs index a43d06f..39b5be8 100644 --- a/src/user/bin/shell.rs +++ b/src/user/bin/shell.rs @@ -58,7 +58,12 @@ pub async fn eventloop() { } fn handle_error(e: Error) { - println!("there was an error! exiting program!"); + if let Error::ApplicationError(s) = e { + println!("there was an error! exiting program!: {}", s); + } else { + println!("there was an error! exiting program!"); + + } } async fn exec() -> Result<(), Error> {