diff --git a/src/chart_manager.rs b/src/chart_manager.rs index 826eaa1..096c058 100644 --- a/src/chart_manager.rs +++ b/src/chart_manager.rs @@ -13,8 +13,7 @@ pub struct ChartManager { impl ChartManager { pub fn new( - func_str: String, min_x: f64, max_x: f64, num_interval: usize, - resolution: usize, + func_str: String, min_x: f64, max_x: f64, num_interval: usize, resolution: usize, ) -> Self { Self { function: Function::from_string(func_str), @@ -28,9 +27,7 @@ impl ChartManager { } #[inline] - fn draw( - &mut self - ) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) { + fn draw(&mut self) -> (Vec<(f64, f64)>, Vec<(f64, f64, f64)>, f64) { let absrange = (self.max_x - self.min_x).abs(); let data: Vec<(f64, f64)> = match self.back_cache.is_valid() { true => self.back_cache.get().clone(), @@ -44,10 +41,7 @@ impl ChartManager { } }; - let filtered_data: Vec<(f64, f64)> = data - .iter() - .map(|(x, y)| (*x, *y)) - .collect(); + let filtered_data: Vec<(f64, f64)> = data.iter().map(|(x, y)| (*x, *y)).collect(); let (rect_data, area): (Vec<(f64, f64, f64)>, f64) = match self.front_cache.is_valid() { true => self.front_cache.get().clone(), @@ -64,14 +58,13 @@ impl ChartManager { #[allow(clippy::too_many_arguments)] 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) { let func_str: String = add_asterisks(func_str_new); let update_func: bool = !self.function.str_compare(func_str.clone()); - let underlying_update = update_func - | (min_x != self.min_x) - | (max_x != self.max_x); + let underlying_update = update_func | (min_x != self.min_x) | (max_x != self.max_x); if underlying_update | (self.resolution != resolution) { self.back_cache.invalidate(); @@ -122,4 +115,4 @@ impl ChartManager { let area: f64 = data2.iter().map(|(_, _, y)| y * step).sum(); // sum of all rectangles' areas (data2, area) } -} \ No newline at end of file +} diff --git a/src/egui_app.rs b/src/egui_app.rs index 4c82b5a..edb6d65 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -1,14 +1,17 @@ use core::num; -use eframe::{egui, epi}; -use egui::{plot::{HLine, Line, Plot, Value, Values, Text}, Pos2}; use crate::chart_manager::ChartManager; -use meval::Expr; -use crate::misc::{add_asterisks, Cache, Function, digits_precision, test_func}; +use crate::misc::{add_asterisks, digits_precision, test_func, Cache, Function}; +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 emath::Rect; -use epaint::{Rounding, RectShape, Stroke}; -use egui::widgets::plot::{Bar, BarChart}; +use epaint::{RectShape, Rounding, Stroke}; +use meval::Expr; pub struct MathApp { func_str: String, @@ -27,24 +30,19 @@ impl Default for MathApp { max_x: 10.0, num_interval: 100, 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 { - fn name(&self) -> &str { - "eframe template" - } + fn name(&self) -> &str { "eframe template" } /// Called once before the first frame. fn setup( - &mut self, - _ctx: &egui::Context, - _frame: &epi::Frame, - _storage: Option<&dyn epi::Storage>, - ) { } - + &mut self, _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. /// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`. @@ -55,7 +53,7 @@ impl epi::App for MathApp { max_x, num_interval, resolution, - chart_manager + chart_manager, } = self; let mut parse_error: String = "".to_string(); @@ -83,13 +81,25 @@ impl epi::App for MathApp { 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 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); ui.label(format!("Area: {}", digits_precision(area, 8))); @@ -101,8 +111,7 @@ impl epi::App for MathApp { if self.num_interval > 0 { plot_ui.bar_chart(barchart); } - }); + }); }); - } } diff --git a/src/lib.rs b/src/lib.rs index 8fb03af..921d143 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ #![allow(clippy::unused_unit)] // Fixes clippy keep complaining about wasm_bindgen #![allow(clippy::type_complexity)] // Clippy, my types are fine. -mod misc; -mod egui_app; mod chart_manager; +mod egui_app; +mod misc; use std::panic; #[cfg(target_arch = "wasm32")] -use eframe::{epi, egui}; +use eframe::{egui, epi}; #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; diff --git a/src/main.rs b/src/main.rs index e1e738c..96df7d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ mod egui_app; // These 2 are needed for rust-analyzer to work in vscode. -mod misc; mod chart_manager; +mod misc; // For running the program natively! (Because why not?) #[cfg(not(target_arch = "wasm32"))] @@ -15,4 +15,4 @@ fn main() { ..Default::default() }; eframe::run_native(Box::new(app), options); -} \ No newline at end of file +}