updated grapher
fixed issue where grapher would fail with functions due to some negative values being undefined
This commit is contained in:
+45
-20
@@ -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<Point>,
|
||||
points: Vec<PointF64>,
|
||||
frame: Vec<Vec<char>>,
|
||||
}
|
||||
|
||||
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<String>) -> 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::<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")));
|
||||
|
||||
self.points.push(Point {
|
||||
if let Ok(y) = fx {
|
||||
self.render_point(PointF64 {
|
||||
x,
|
||||
y: y as i16,
|
||||
y,
|
||||
})
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
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<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| {
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,12 @@ pub async fn eventloop() {
|
||||
}
|
||||
|
||||
fn handle_error(e: Error) {
|
||||
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> {
|
||||
|
||||
Reference in New Issue
Block a user