This commit is contained in:
Simon Gardling 2022-02-23 15:37:58 -05:00
parent e049038813
commit e7583b5497
4 changed files with 43 additions and 41 deletions

View File

@ -13,8 +13,7 @@ pub struct ChartManager {
impl ChartManager { impl ChartManager {
pub fn new( pub fn new(
func_str: String, min_x: f64, max_x: f64, num_interval: usize, func_str: String, min_x: f64, max_x: f64, num_interval: usize, resolution: usize,
resolution: usize,
) -> Self { ) -> Self {
Self { Self {
function: Function::from_string(func_str), function: Function::from_string(func_str),
@ -28,9 +27,7 @@ impl ChartManager {
} }
#[inline] #[inline]
fn draw( fn draw(&mut self) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) {
&mut self
) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) {
let absrange = (self.max_x - self.min_x).abs(); let absrange = (self.max_x - self.min_x).abs();
let data: Vec<(f64, f64)> = match self.back_cache.is_valid() { let data: Vec<(f64, f64)> = match self.back_cache.is_valid() {
true => self.back_cache.get().clone(), true => self.back_cache.get().clone(),
@ -44,10 +41,7 @@ impl ChartManager {
} }
}; };
let filtered_data: Vec<(f64, f64)> = data let filtered_data: Vec<(f64, f64)> = data.iter().map(|(x, y)| (*x, *y)).collect();
.iter()
.map(|(x, y)| (*x, *y))
.collect();
let (rect_data, area): (Vec<(f64, f64, f64)>, f64) = match self.front_cache.is_valid() { let (rect_data, area): (Vec<(f64, f64, f64)>, f64) = match self.front_cache.is_valid() {
true => self.front_cache.get().clone(), true => self.front_cache.get().clone(),
@ -64,14 +58,13 @@ impl ChartManager {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn update( pub fn update(
&mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize, resolution: usize, &mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize,
resolution: usize,
) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) { ) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) {
let func_str: String = add_asterisks(func_str_new); let func_str: String = add_asterisks(func_str_new);
let update_func: bool = !self.function.str_compare(func_str.clone()); let update_func: bool = !self.function.str_compare(func_str.clone());
let underlying_update = update_func let underlying_update = update_func | (min_x != self.min_x) | (max_x != self.max_x);
| (min_x != self.min_x)
| (max_x != self.max_x);
if underlying_update | (self.resolution != resolution) { if underlying_update | (self.resolution != resolution) {
self.back_cache.invalidate(); self.back_cache.invalidate();

View File

@ -1,14 +1,17 @@
use core::num; use core::num;
use eframe::{egui, epi};
use egui::{plot::{HLine, Line, Plot, Value, Values, Text}, Pos2};
use crate::chart_manager::ChartManager; use crate::chart_manager::ChartManager;
use meval::Expr; use crate::misc::{add_asterisks, digits_precision, test_func, Cache, Function};
use crate::misc::{add_asterisks, Cache, Function, digits_precision, test_func}; use eframe::{egui, epi};
use egui::widgets::plot::{Bar, BarChart};
use egui::{
plot::{HLine, Line, Plot, Text, Value, Values},
Pos2,
};
use egui::{Color32, ColorImage, Ui}; use egui::{Color32, ColorImage, Ui};
use emath::Rect; use emath::Rect;
use epaint::{Rounding, RectShape, Stroke}; use epaint::{RectShape, Rounding, Stroke};
use egui::widgets::plot::{Bar, BarChart}; use meval::Expr;
pub struct MathApp { pub struct MathApp {
func_str: String, func_str: String,
@ -27,24 +30,19 @@ impl Default for MathApp {
max_x: 10.0, max_x: 10.0,
num_interval: 100, num_interval: 100,
resolution: 10000, resolution: 10000,
chart_manager: ChartManager::new("x^2".to_string(), -10.0, 10.0, 100, 10000) chart_manager: ChartManager::new("x^2".to_string(), -10.0, 10.0, 100, 10000),
} }
} }
} }
impl epi::App for MathApp { impl epi::App for MathApp {
fn name(&self) -> &str { fn name(&self) -> &str { "eframe template" }
"eframe template"
}
/// Called once before the first frame. /// Called once before the first frame.
fn setup( fn setup(
&mut self, &mut self, _ctx: &egui::Context, _frame: &epi::Frame, _storage: Option<&dyn epi::Storage>,
_ctx: &egui::Context, ) {
_frame: &epi::Frame, }
_storage: Option<&dyn epi::Storage>,
) { }
/// Called each time the UI needs repainting, which may be many times per second. /// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`. /// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
@ -55,7 +53,7 @@ impl epi::App for MathApp {
max_x, max_x,
num_interval, num_interval,
resolution, resolution,
chart_manager chart_manager,
} = self; } = self;
let mut parse_error: String = "".to_string(); let mut parse_error: String = "".to_string();
@ -83,13 +81,25 @@ impl epi::App for MathApp {
return; return;
} }
let (filtered_data, rect_data, area) = chart_manager.update(self.func_str.clone(), self.min_x, self.max_x, self.num_interval, self.resolution); let (filtered_data, rect_data, area) = chart_manager.update(
self.func_str.clone(),
self.min_x,
self.max_x,
self.num_interval,
self.resolution,
);
let filtered_data_values = filtered_data.iter().map(|(x, y)| Value::new(*x, *y)).collect(); let filtered_data_values = filtered_data
.iter()
.map(|(x, y)| Value::new(*x, *y))
.collect();
let curve = Line::new(Values::from_values(filtered_data_values)).color(Color32::RED); let curve = Line::new(Values::from_values(filtered_data_values)).color(Color32::RED);
let bars = rect_data.iter().map(|(_, x2, y)| Bar::new(*x2, *y)).collect(); let bars = rect_data
.iter()
.map(|(_, x2, y)| Bar::new(*x2, *y))
.collect();
let barchart = BarChart::new(bars).color(Color32::BLUE); let barchart = BarChart::new(bars).color(Color32::BLUE);
ui.label(format!("Area: {}", digits_precision(area, 8))); ui.label(format!("Area: {}", digits_precision(area, 8)));
@ -103,6 +113,5 @@ impl epi::App for MathApp {
} }
}); });
}); });
} }
} }

View File

@ -1,13 +1,13 @@
#![allow(clippy::unused_unit)] // Fixes clippy keep complaining about wasm_bindgen #![allow(clippy::unused_unit)] // Fixes clippy keep complaining about wasm_bindgen
#![allow(clippy::type_complexity)] // Clippy, my types are fine. #![allow(clippy::type_complexity)] // Clippy, my types are fine.
mod misc;
mod egui_app;
mod chart_manager; mod chart_manager;
mod egui_app;
mod misc;
use std::panic; use std::panic;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use eframe::{epi, egui}; use eframe::{egui, epi};
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;

View File

@ -1,8 +1,8 @@
mod egui_app; mod egui_app;
// These 2 are needed for rust-analyzer to work in vscode. // These 2 are needed for rust-analyzer to work in vscode.
mod misc;
mod chart_manager; mod chart_manager;
mod misc;
// For running the program natively! (Because why not?) // For running the program natively! (Because why not?)
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]