improvements

This commit is contained in:
Simon Gardling 2022-02-25 12:22:57 -05:00
parent 274d072bfe
commit 14fcfc8e54
2 changed files with 33 additions and 5 deletions

View File

@ -105,7 +105,7 @@ impl epi::App for MathApp {
ui.label("- signum, min, max");
});
let mut new_func_data: Vec<(String, bool)> = Vec::new();
let mut new_func_data: Vec<(String, bool, bool)> = Vec::new();
let mut parse_error: String = "".to_string();
egui::SidePanel::left("side_panel")
.resizable(false)
@ -132,11 +132,13 @@ impl epi::App for MathApp {
});
let func_test_output = test_func(func_str.clone());
let mut got_error: bool = false;
if !func_test_output.is_empty() {
parse_error += &func_test_output;
got_error = true;
}
new_func_data.push((func_str, integral_toggle));
new_func_data.push((func_str, integral_toggle, got_error));
}
let min_x_old = *min_x;
@ -191,15 +193,16 @@ impl epi::App for MathApp {
let mut i: usize = 0;
for function in functions.iter_mut() {
let (func_str, integral_toggle) = (new_func_data[i].0.clone(), new_func_data[i].1);
let (func_str, integral_toggle, got_error) = (new_func_data[i].0.clone(), new_func_data[i].1, new_func_data[i].2);
let integral: bool = if integral_toggle {
!function.is_integral()
} else {
function.is_integral()
};
function.update(func_str, *min_x, *max_x, integral, Some(*integral_min_x), Some(*integral_max_x), Some(*integral_num));
function.update(func_str, *min_x, *max_x, integral, Some(*integral_min_x), Some(*integral_max_x), Some(*integral_num), got_error);
i += 1;
}
});
@ -218,6 +221,10 @@ impl epi::App for MathApp {
.include_y(0)
.show(ui, |plot_ui| {
for function in self.functions.iter_mut() {
if function.is_broken() {
continue;
}
let output = function.run();
let back = output.get_back();
plot_ui.line(Line::new(Values::from_values(back)).color(Color32::RED));

View File

@ -51,6 +51,7 @@ pub struct Function {
integral_min_x: f64,
integral_max_x: f64,
integral_num: usize,
broken_state: bool,
}
impl Function {
@ -91,6 +92,7 @@ impl Function {
Some(x) => x,
None => 0,
},
broken_state: false,
}
}
@ -102,7 +104,14 @@ impl Function {
pub fn update(
&mut self, func_str: String, min_x: f64, max_x: f64, integral: bool,
integral_min_x: Option<f64>, integral_max_x: Option<f64>, integral_num: Option<usize>,
broken_state: bool,
) {
if broken_state {
self.func_str = func_str.clone();
self.broken_state = true;
return;
}
// If the function string changes, just wipe and restart from scratch
if func_str != self.func_str {
*self = Self::new(
@ -157,6 +166,9 @@ impl Function {
#[inline]
pub fn is_integral(&self) -> bool { self.integral }
#[inline]
pub fn is_broken(&self) -> bool { self.broken_state }
#[inline]
pub fn run(&mut self) -> FunctionOutput {
let front_values: Vec<Value> = match self.back_cache.is_valid() {
@ -208,6 +220,15 @@ impl Function {
if !self.integral {
panic!("integral_rectangles called, but self.integral is false!");
}
if self.integral_min_x.is_nan() {
panic!("integral_min_x is NaN")
}
if self.integral_max_x.is_nan() {
panic!("integral_max_x is NaN")
}
let step = self.get_step();
let half_step = step / 2.0;