From da140f1a066857a2efc530c4335a3a685014ecdc Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 28 Feb 2022 15:09:07 -0500 Subject: [PATCH] simplify function output handling --- src/egui_app.rs | 17 +++++++---------- src/function.rs | 39 +++++++++------------------------------ 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/egui_app.rs b/src/egui_app.rs index 62a8de8..d5c6f91 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -3,7 +3,7 @@ use std::ops::RangeInclusive; use crate::function::Function; use crate::misc::{add_asterisks, digits_precision, test_func}; use eframe::{egui, epi}; -use egui::plot::{Line, Plot, Values}; +use egui::plot::Plot; use egui::widgets::plot::BarChart; use egui::widgets::Button; use egui::{Color32, FontData, FontFamily, Vec2}; @@ -246,18 +246,15 @@ impl epi::App for MathApp { function.update_bounds(minx_bounds, maxx_bounds, available_width); - let output = function.run(); - let back = output.get_back(); - plot_ui.line(Line::new(Values::from_values(back)).color(Color32::RED)); + let (back_values, bars) = function.run(); + plot_ui.line(back_values.color(Color32::RED)); - if output.has_integral() { - let (bars, area) = output.get_front(); - let bar_chart = - BarChart::new(bars.clone()).color(Color32::BLUE).width(step); - plot_ui.bar_chart(bar_chart); + if bars.is_some() { + let (bars, area) = bars.unwrap(); + plot_ui.bar_chart(BarChart::new(bars).color(Color32::BLUE).width(step)); area_list.push(digits_precision(area, 8)) } else { - area_list.push(0.0); + area_list.push(f64::NAN); } i += 1; } diff --git a/src/function.rs b/src/function.rs index 709e23d..2f732e6 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,33 +1,12 @@ #[allow(unused_imports)] use crate::misc::debug_log; -use eframe::egui::{plot::Value, widgets::plot::Bar}; +use eframe::egui::{ + plot::{Line, Value, Values}, + widgets::plot::Bar, +}; use meval::Expr; -// Struct that stores and manages the output of a function -pub struct FunctionOutput { - // The actual line graph - back: Vec, - - // Integral information - front: Option<(Vec, f64)>, -} - -impl FunctionOutput { - pub fn new(back: Vec, front: Option<(Vec, f64)>) -> Self { Self { back, front } } - - pub fn get_back(&self) -> Vec { self.back.clone() } - - pub fn get_front(&self) -> (Vec, f64) { - match &self.front { - Some(x) => (x.0.clone(), x.1), - None => panic!(""), - } - } - - pub fn has_integral(&self) -> bool { self.front.is_some() } -} - pub struct Function { function: Box f64>, pub(crate) func_str: String, @@ -199,8 +178,8 @@ impl Function { pub fn is_integral(&self) -> bool { self.integral } - pub fn run(&mut self) -> FunctionOutput { - let back_values: Vec = match self.back_cache.is_some() { + pub fn run(&mut self) -> (Line, Option<(Vec, f64)>) { + let back_values: Line = Line::new(Values::from_values(match self.back_cache.is_some() { true => { debug_log("back_cache: using"); self.back_cache.as_ref().unwrap().clone() @@ -219,7 +198,7 @@ impl Function { self.back_cache = Some(back_data.clone()); back_data } - }; + })); if self.integral { let front_bars: (Vec, f64) = match self.front_cache.is_some() { @@ -239,9 +218,9 @@ impl Function { output } }; - FunctionOutput::new(back_values, Some(front_bars)) + (back_values, Some(front_bars)) } else { - FunctionOutput::new(back_values, None) + (back_values, None) } }