turn some helper functions into traits
This commit is contained in:
parent
2471dff87e
commit
2e2f28f60b
@ -641,7 +641,7 @@ impl epi::App for MathApp {
|
|||||||
// Display Area and time of last frame
|
// Display Area and time of last frame
|
||||||
ui.label(format!(
|
ui.label(format!(
|
||||||
"Area: {} Took: {:?}",
|
"Area: {} Took: {:?}",
|
||||||
option_vec_printer(self.last_info.0.clone()),
|
option_vec_printer(&self.last_info.0),
|
||||||
self.last_info.1
|
self.last_info.1
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
|||||||
@ -305,17 +305,23 @@ impl FunctionEntry {
|
|||||||
let step = (settings.integral_min_x - settings.integral_max_x).abs()
|
let step = (settings.integral_min_x - settings.integral_max_x).abs()
|
||||||
/ (settings.integral_num as f64);
|
/ (settings.integral_num as f64);
|
||||||
// Plot back data
|
// Plot back data
|
||||||
plot_ui.line(
|
if let Some(back_data) = &self.back_data {
|
||||||
vec_tuple_to_line(self.back_data.clone().unwrap())
|
plot_ui.line(
|
||||||
.color(Color32::RED)
|
back_data
|
||||||
.name(func_str),
|
.clone()
|
||||||
);
|
.to_line()
|
||||||
|
.color(Color32::RED)
|
||||||
|
.name(func_str),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Plot derivative data
|
// Plot derivative data
|
||||||
if self.derivative {
|
if self.derivative {
|
||||||
if let Some(derivative_data) = self.derivative_data.clone() {
|
if let Some(derivative_data) = &self.derivative_data {
|
||||||
plot_ui.line(
|
plot_ui.line(
|
||||||
vec_tuple_to_line(derivative_data)
|
derivative_data
|
||||||
|
.clone()
|
||||||
|
.to_line()
|
||||||
.color(Color32::GREEN)
|
.color(Color32::GREEN)
|
||||||
.name(derivative_str),
|
.name(derivative_str),
|
||||||
);
|
);
|
||||||
@ -326,7 +332,9 @@ impl FunctionEntry {
|
|||||||
if settings.do_extrema {
|
if settings.do_extrema {
|
||||||
if let Some(extrema_data) = &self.extrema_data {
|
if let Some(extrema_data) = &self.extrema_data {
|
||||||
plot_ui.points(
|
plot_ui.points(
|
||||||
vec_tuple_to_points(extrema_data.clone())
|
extrema_data
|
||||||
|
.clone()
|
||||||
|
.to_points()
|
||||||
.color(Color32::YELLOW)
|
.color(Color32::YELLOW)
|
||||||
.name("Extrema")
|
.name("Extrema")
|
||||||
.radius(5.0), // Radius of points of Extrema
|
.radius(5.0), // Radius of points of Extrema
|
||||||
@ -338,7 +346,9 @@ impl FunctionEntry {
|
|||||||
if settings.do_roots {
|
if settings.do_roots {
|
||||||
if let Some(roots_data) = &self.roots_data {
|
if let Some(roots_data) = &self.roots_data {
|
||||||
plot_ui.points(
|
plot_ui.points(
|
||||||
vec_tuple_to_points(roots_data.clone())
|
roots_data
|
||||||
|
.clone()
|
||||||
|
.to_points()
|
||||||
.color(Color32::LIGHT_BLUE)
|
.color(Color32::LIGHT_BLUE)
|
||||||
.name("Root")
|
.name("Root")
|
||||||
.radius(5.0), // Radius of points of Roots
|
.radius(5.0), // Radius of points of Roots
|
||||||
@ -349,7 +359,7 @@ impl FunctionEntry {
|
|||||||
// Plot integral data
|
// Plot integral data
|
||||||
if let Some(integral_data) = &self.integral_data {
|
if let Some(integral_data) = &self.integral_data {
|
||||||
plot_ui.bar_chart(
|
plot_ui.bar_chart(
|
||||||
BarChart::new(integral_data.clone().0)
|
BarChart::new(integral_data.0.clone())
|
||||||
.color(Color32::BLUE)
|
.color(Color32::BLUE)
|
||||||
.width(step),
|
.width(step),
|
||||||
);
|
);
|
||||||
|
|||||||
24
src/misc.rs
24
src/misc.rs
@ -177,13 +177,21 @@ impl From<Vec<f64>> for SteppedVector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts Vector of egui `Value` into `Points`
|
/// Implements traits that are useful when dealing with Vectors of egui's
|
||||||
pub fn vec_tuple_to_points(data: Vec<EguiValue>) -> Points {
|
/// `Value`
|
||||||
Points::new(Values::from_values(data))
|
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`
|
impl EguiHelper for Vec<EguiValue> {
|
||||||
pub fn vec_tuple_to_line(data: Vec<EguiValue>) -> Line { Line::new(Values::from_values(data)) }
|
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)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct JsonFileOutput {
|
pub struct JsonFileOutput {
|
||||||
@ -298,7 +306,7 @@ fn newtons_method(
|
|||||||
|
|
||||||
/// Inputs `Vec<Option<T>>` and outputs a `String` containing a pretty
|
/// Inputs `Vec<Option<T>>` and outputs a `String` containing a pretty
|
||||||
/// representation of the Vector
|
/// 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
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
T: Clone,
|
T: Clone,
|
||||||
@ -424,7 +432,7 @@ mod tests {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
for (key, value) in values_strings {
|
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([
|
let values_nums = HashMap::from([
|
||||||
@ -435,7 +443,7 @@ mod tests {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
for (key, value) in values_nums {
|
for (key, value) in values_nums {
|
||||||
assert_eq!(option_vec_printer(key), value);
|
assert_eq!(option_vec_printer(&key), value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user