bunch of refactoring
This commit is contained in:
parent
0b29af8515
commit
cee67810b4
@ -33,7 +33,7 @@ const INTEGRAL_X_RANGE: RangeInclusive<f64> = INTEGRAL_X_MIN..=INTEGRAL_X_MAX;
|
|||||||
|
|
||||||
// Default values
|
// Default values
|
||||||
const DEFAULT_FUNCION: &str = "x^2";
|
const DEFAULT_FUNCION: &str = "x^2";
|
||||||
const DEFAULT_RIEMANN: RiemannSum = RiemannSum::Left;
|
pub const DEFAULT_RIEMANN: RiemannSum = RiemannSum::Left;
|
||||||
const DEFAULT_MIN_X: f64 = -10.0;
|
const DEFAULT_MIN_X: f64 = -10.0;
|
||||||
const DEFAULT_MAX_X: f64 = 10.0;
|
const DEFAULT_MAX_X: f64 = 10.0;
|
||||||
const DEFAULT_INTEGRAL_NUM: usize = 100;
|
const DEFAULT_INTEGRAL_NUM: usize = 100;
|
||||||
@ -135,17 +135,7 @@ pub struct MathApp {
|
|||||||
impl Default for MathApp {
|
impl Default for MathApp {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
functions: vec![Function::new(
|
functions: vec![Function::empty().integral(true)],
|
||||||
String::from(DEFAULT_FUNCION),
|
|
||||||
DEFAULT_MIN_X,
|
|
||||||
DEFAULT_MAX_X,
|
|
||||||
100, // Doesn't matter as it will be overwritten
|
|
||||||
true, // Enables integral
|
|
||||||
Some(DEFAULT_MIN_X),
|
|
||||||
Some(DEFAULT_MAX_X),
|
|
||||||
Some(DEFAULT_INTEGRAL_NUM),
|
|
||||||
Some(DEFAULT_RIEMANN),
|
|
||||||
)],
|
|
||||||
func_strs: vec![String::from(DEFAULT_FUNCION)],
|
func_strs: vec![String::from(DEFAULT_FUNCION)],
|
||||||
last_error: String::new(),
|
last_error: String::new(),
|
||||||
font: FontData::from_static(&FONT_FILE),
|
font: FontData::from_static(&FONT_FILE),
|
||||||
@ -245,10 +235,6 @@ impl MathApp {
|
|||||||
integral_enabled
|
integral_enabled
|
||||||
};
|
};
|
||||||
|
|
||||||
if integral {
|
|
||||||
using_integral = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !self.func_strs[i].is_empty() {
|
if !self.func_strs[i].is_empty() {
|
||||||
let proc_func_str = add_asterisks(self.func_strs[i].clone());
|
let proc_func_str = add_asterisks(self.func_strs[i].clone());
|
||||||
let func_test_output = test_func(proc_func_str.clone());
|
let func_test_output = test_func(proc_func_str.clone());
|
||||||
@ -264,6 +250,9 @@ impl MathApp {
|
|||||||
Some(self.settings.sum),
|
Some(self.settings.sum),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if integral {
|
||||||
|
using_integral = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
function.empty_func_str();
|
function.empty_func_str();
|
||||||
}
|
}
|
||||||
@ -336,21 +325,8 @@ impl epi::App for MathApp {
|
|||||||
.on_hover_text("Create and graph new function")
|
.on_hover_text("Create and graph new function")
|
||||||
.clicked()
|
.clicked()
|
||||||
{
|
{
|
||||||
self.functions.push({
|
self.functions
|
||||||
let mut function = Function::new(
|
.push(Function::empty().update_riemann(self.settings.sum));
|
||||||
String::from(DEFAULT_FUNCION),
|
|
||||||
-1.0, // Doesn't matter, updated later
|
|
||||||
1.0, // Doesn't matter, updated later
|
|
||||||
100, // Doesn't matter, updated later
|
|
||||||
false,
|
|
||||||
None, // Doesn't matter, updated later
|
|
||||||
None, // Doesn't matter, updated later
|
|
||||||
None, // Doesn't matter, updated later
|
|
||||||
Some(self.settings.sum),
|
|
||||||
);
|
|
||||||
function.empty_func_str();
|
|
||||||
function
|
|
||||||
});
|
|
||||||
self.func_strs.push(String::new());
|
self.func_strs.push(String::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
109
src/function.rs
109
src/function.rs
@ -38,38 +38,24 @@ pub struct Function {
|
|||||||
sum: RiemannSum,
|
sum: RiemannSum,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Function {
|
fn default_function(x: f64) -> f64 { x.powi(2) }
|
||||||
pub fn new(
|
|
||||||
func_str: String, min_x: f64, max_x: f64, pixel_width: usize, integral: bool,
|
|
||||||
integral_min_x: Option<f64>, integral_max_x: Option<f64>, integral_num: Option<usize>,
|
|
||||||
sum: Option<RiemannSum>,
|
|
||||||
) -> Self {
|
|
||||||
// Makes sure proper arguments are passed when integral is enabled
|
|
||||||
if integral {
|
|
||||||
integral_min_x
|
|
||||||
.expect("Invalid arguments: integral_min_x is None, but integral is enabled.");
|
|
||||||
integral_max_x
|
|
||||||
.expect("Invalid arguments: integral_max_x is None, but integral is enabled.");
|
|
||||||
integral_num
|
|
||||||
.expect("Invalid arguments: integral_num is None, but integral is enabled.");
|
|
||||||
sum.expect("Invalid arguments: sum is None, but integral is enabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
let expr: Expr = func_str.parse().unwrap();
|
impl Function {
|
||||||
let func = expr.bind("x").unwrap();
|
// Creates Empty Function instance
|
||||||
|
pub fn empty() -> Self {
|
||||||
Self {
|
Self {
|
||||||
function: Box::new(func),
|
function: Box::new(default_function),
|
||||||
func_str,
|
func_str: String::new(),
|
||||||
min_x,
|
min_x: -1.0,
|
||||||
max_x,
|
max_x: 1.0,
|
||||||
pixel_width,
|
pixel_width: 100,
|
||||||
back_cache: None,
|
back_cache: None,
|
||||||
front_cache: None,
|
front_cache: None,
|
||||||
integral,
|
integral: false,
|
||||||
integral_min_x: integral_min_x.unwrap_or(f64::NAN),
|
integral_min_x: f64::NAN,
|
||||||
integral_max_x: integral_max_x.unwrap_or(f64::NAN),
|
integral_max_x: f64::NAN,
|
||||||
integral_num: integral_num.unwrap_or(0),
|
integral_num: 0,
|
||||||
sum: sum.unwrap_or(RiemannSum::Left),
|
sum: crate::egui_app::DEFAULT_RIEMANN,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,25 +66,15 @@ impl Function {
|
|||||||
&mut self, func_str: String, integral: bool, integral_min_x: Option<f64>,
|
&mut self, func_str: String, integral: bool, integral_min_x: Option<f64>,
|
||||||
integral_max_x: Option<f64>, integral_num: Option<usize>, sum: Option<RiemannSum>,
|
integral_max_x: Option<f64>, integral_num: Option<usize>, sum: Option<RiemannSum>,
|
||||||
) {
|
) {
|
||||||
if func_str.is_empty() {
|
|
||||||
self.func_str = func_str;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the function string changes, just wipe and restart from scratch
|
// If the function string changes, just wipe and restart from scratch
|
||||||
if func_str != self.func_str {
|
if func_str != self.func_str {
|
||||||
*self = Self::new(
|
self.func_str = func_str.clone();
|
||||||
func_str,
|
self.function = Box::new({
|
||||||
self.min_x,
|
let expr: Expr = func_str.parse().unwrap();
|
||||||
self.max_x,
|
expr.bind("x").unwrap()
|
||||||
self.pixel_width,
|
});
|
||||||
integral,
|
self.back_cache = None;
|
||||||
integral_min_x,
|
self.front_cache = None;
|
||||||
integral_max_x,
|
|
||||||
integral_num,
|
|
||||||
sum,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.integral = integral;
|
self.integral = integral;
|
||||||
@ -173,20 +149,21 @@ impl Function {
|
|||||||
self.back_cache.as_ref().unwrap().clone()
|
self.back_cache.as_ref().unwrap().clone()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if self.integral {
|
match self.integral {
|
||||||
let front_bars: (BarChart, f64) = {
|
true => {
|
||||||
if self.front_cache.is_none() {
|
let front_bars: (BarChart, f64) = {
|
||||||
let (data, area) = self.integral_rectangles();
|
if self.front_cache.is_none() {
|
||||||
self.front_cache =
|
let (data, area) = self.integral_rectangles();
|
||||||
Some((data.iter().map(|(x, y)| Bar::new(*x, *y)).collect(), area));
|
self.front_cache =
|
||||||
}
|
Some((data.iter().map(|(x, y)| Bar::new(*x, *y)).collect(), area));
|
||||||
let cache = self.front_cache.as_ref().unwrap();
|
}
|
||||||
(BarChart::new(cache.0.clone()), cache.1)
|
let cache = self.front_cache.as_ref().unwrap();
|
||||||
};
|
(BarChart::new(cache.0.clone()), cache.1)
|
||||||
|
};
|
||||||
|
|
||||||
(back_values, Some(front_bars))
|
(back_values, Some(front_bars))
|
||||||
} else {
|
}
|
||||||
(back_values, None)
|
false => (back_values, None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,5 +211,21 @@ impl Function {
|
|||||||
(data2, area)
|
(data2, area)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set func_str to an empty string
|
||||||
pub fn empty_func_str(&mut self) { self.func_str = String::new(); }
|
pub fn empty_func_str(&mut self) { self.func_str = String::new(); }
|
||||||
|
|
||||||
|
// Updates riemann value and invalidates front_cache if needed
|
||||||
|
pub fn update_riemann(mut self, riemann: RiemannSum) -> Self {
|
||||||
|
if self.sum != riemann {
|
||||||
|
self.sum = riemann;
|
||||||
|
self.front_cache = None;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toggles integral
|
||||||
|
pub fn integral(mut self, integral: bool) -> Self {
|
||||||
|
self.integral = integral;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user