updated grapher
fixed issue where grapher would fail with functions due to some negative values being undefined
This commit is contained in:
+47
-22
@@ -1,5 +1,5 @@
|
|||||||
use alloc::string::{String, ToString};
|
use alloc::string::{String, ToString};
|
||||||
use alloc::vec;
|
use alloc::{format, vec};
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
@@ -9,41 +9,56 @@ use crate::std::application::{Application, Error};
|
|||||||
use crate::std::frame::Element;
|
use crate::std::frame::Element;
|
||||||
use crate::std::io::{Screen, Stdin};
|
use crate::std::io::{Screen, Stdin};
|
||||||
|
|
||||||
|
const OFFSET_X: i64 = 40;
|
||||||
|
const OFFSET_Y: i64 = 12;
|
||||||
|
|
||||||
pub struct Grapher {
|
pub struct Grapher {
|
||||||
points: Vec<Point>,
|
points: Vec<PointF64>,
|
||||||
|
frame: Vec<Vec<char>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct PointF64 {
|
||||||
x: i16,
|
x: f64,
|
||||||
y: i16,
|
y: f64,
|
||||||
}
|
}
|
||||||
|
struct PointI64 {
|
||||||
|
x: i64,
|
||||||
|
y: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Application for Grapher {
|
impl Application for Grapher {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
points: Vec::new(),
|
points: Vec::new(),
|
||||||
|
frame: vec![vec![' '; 80]; 25],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
|
async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
|
||||||
let mut equation: String = args.into_iter().collect();
|
let mut equation: String = args.into_iter().collect();
|
||||||
use super::calc;
|
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| {
|
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::<String>();
|
}).collect::<String>();
|
||||||
|
|
||||||
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();
|
Screen::application_mode();
|
||||||
self.render();
|
self.display();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match Stdin::keystroke().await {
|
match Stdin::keystroke().await {
|
||||||
@@ -58,17 +73,27 @@ impl Application for Grapher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Grapher {
|
impl Grapher {
|
||||||
fn render(&self) {
|
|
||||||
let mut frame: Vec<Vec<char>> = vec![vec![' '; 80]; 25];
|
|
||||||
|
|
||||||
self.points.iter().filter(|i| i.x >= -40 && i.x < 40 && i.y >= -12 && i.y <= 12).for_each(|i| {
|
fn render_point(&mut self, point: PointF64) {
|
||||||
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] = '*';
|
|
||||||
});
|
|
||||||
|
|
||||||
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));
|
elem.render((0, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,12 @@ pub async fn eventloop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_error(e: Error) {
|
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> {
|
async fn exec() -> Result<(), Error> {
|
||||||
|
|||||||
Reference in New Issue
Block a user