turn some helper functions into traits

This commit is contained in:
Simon Gardling 2022-03-30 11:13:04 -04:00
parent 2471dff87e
commit 2e2f28f60b
3 changed files with 37 additions and 19 deletions

View File

@ -641,7 +641,7 @@ impl epi::App for MathApp {
// Display Area and time of last frame
ui.label(format!(
"Area: {} Took: {:?}",
option_vec_printer(self.last_info.0.clone()),
option_vec_printer(&self.last_info.0),
self.last_info.1
));
});

View File

@ -305,17 +305,23 @@ impl FunctionEntry {
let step = (settings.integral_min_x - settings.integral_max_x).abs()
/ (settings.integral_num as f64);
// Plot back data
plot_ui.line(
vec_tuple_to_line(self.back_data.clone().unwrap())
.color(Color32::RED)
.name(func_str),
);
if let Some(back_data) = &self.back_data {
plot_ui.line(
back_data
.clone()
.to_line()
.color(Color32::RED)
.name(func_str),
);
}
// Plot derivative data
if self.derivative {
if let Some(derivative_data) = self.derivative_data.clone() {
if let Some(derivative_data) = &self.derivative_data {
plot_ui.line(
vec_tuple_to_line(derivative_data)
derivative_data
.clone()
.to_line()
.color(Color32::GREEN)
.name(derivative_str),
);
@ -326,7 +332,9 @@ impl FunctionEntry {
if settings.do_extrema {
if let Some(extrema_data) = &self.extrema_data {
plot_ui.points(
vec_tuple_to_points(extrema_data.clone())
extrema_data
.clone()
.to_points()
.color(Color32::YELLOW)
.name("Extrema")
.radius(5.0), // Radius of points of Extrema
@ -338,7 +346,9 @@ impl FunctionEntry {
if settings.do_roots {
if let Some(roots_data) = &self.roots_data {
plot_ui.points(
vec_tuple_to_points(roots_data.clone())
roots_data
.clone()
.to_points()
.color(Color32::LIGHT_BLUE)
.name("Root")
.radius(5.0), // Radius of points of Roots
@ -349,7 +359,7 @@ impl FunctionEntry {
// Plot integral data
if let Some(integral_data) = &self.integral_data {
plot_ui.bar_chart(
BarChart::new(integral_data.clone().0)
BarChart::new(integral_data.0.clone())
.color(Color32::BLUE)
.width(step),
);

View File

@ -177,13 +177,21 @@ impl From<Vec<f64>> for SteppedVector {
}
}
/// Converts Vector of egui `Value` into `Points`
pub fn vec_tuple_to_points(data: Vec<EguiValue>) -> Points {
Points::new(Values::from_values(data))
/// Implements traits that are useful when dealing with Vectors of egui's
/// `Value`
pub trait EguiHelper {
/// Converts to `egui::plot::Line`
fn to_line(&self) -> Line;
/// Converts to `egui::plot::Points`
fn to_points(&self) -> Points;
}
/// Converts Vector of egui `Value` into `Line`
pub fn vec_tuple_to_line(data: Vec<EguiValue>) -> Line { Line::new(Values::from_values(data)) }
impl EguiHelper for Vec<EguiValue> {
fn to_line(&self) -> Line { Line::new(Values::from_values(self.clone())) }
fn to_points(&self) -> Points { Points::new(Values::from_values(self.clone())) }
}
#[derive(PartialEq, Debug)]
pub struct JsonFileOutput {
@ -298,7 +306,7 @@ fn newtons_method(
/// Inputs `Vec<Option<T>>` and outputs a `String` containing a pretty
/// representation of the Vector
pub fn option_vec_printer<T: ToString>(data: Vec<Option<T>>) -> String
pub fn option_vec_printer<T: ToString>(data: &Vec<Option<T>>) -> String
where
T: ToString,
T: Clone,
@ -424,7 +432,7 @@ mod tests {
]);
for (key, value) in values_strings {
assert_eq!(option_vec_printer(key), value);
assert_eq!(option_vec_printer(&key), value);
}
let values_nums = HashMap::from([
@ -435,7 +443,7 @@ mod tests {
]);
for (key, value) in values_nums {
assert_eq!(option_vec_printer(key), value);
assert_eq!(option_vec_printer(&key), value);
}
}