simplify function output handling

This commit is contained in:
Simon Gardling 2022-02-28 15:09:07 -05:00
parent 4371c25fb5
commit da140f1a06
2 changed files with 16 additions and 40 deletions

View File

@ -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;
}

View File

@ -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<Value>,
// Integral information
front: Option<(Vec<Bar>, f64)>,
}
impl FunctionOutput {
pub fn new(back: Vec<Value>, front: Option<(Vec<Bar>, f64)>) -> Self { Self { back, front } }
pub fn get_back(&self) -> Vec<Value> { self.back.clone() }
pub fn get_front(&self) -> (Vec<Bar>, 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<dyn Fn(f64) -> 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<Value> = match self.back_cache.is_some() {
pub fn run(&mut self) -> (Line, Option<(Vec<Bar>, 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<Bar>, 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)
}
}