FunctionEntry: generate_plot_data helper

This commit is contained in:
Simon Gardling 2025-12-05 20:14:48 -05:00
parent 2d7c987f11
commit c7760e2123
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -276,6 +276,17 @@ impl FunctionEntry {
.collect()
}
/// Generates plot data for a given derivative level over the resolution iterator
fn generate_plot_data(&mut self, derivative: usize, resolution_iter: &[f64]) -> Vec<PlotPoint> {
if derivative > 0 {
self.function.generate_derivative(derivative);
}
resolution_iter
.iter()
.map(|&x| PlotPoint::new(x, self.function.get(derivative, x)))
.collect()
}
/// Does the calculations and stores results in `self`
pub fn calculate(
&mut self,
@ -304,32 +315,17 @@ impl FunctionEntry {
}
if self.back_data.is_empty() {
let data: Vec<PlotPoint> = resolution_iter
.clone()
.into_iter()
.map(|x| PlotPoint::new(x, self.function.get(0, x)))
.collect();
debug_assert_eq!(data.len(), settings.plot_width + 1);
self.back_data = data;
self.back_data = self.generate_plot_data(0, &resolution_iter);
debug_assert_eq!(self.back_data.len(), settings.plot_width + 1);
}
if self.derivative_data.is_empty() {
self.function.generate_derivative(1);
let data: Vec<PlotPoint> = resolution_iter
.clone()
.into_iter()
.map(|x| PlotPoint::new(x, self.function.get(1, x)))
.collect();
debug_assert_eq!(data.len(), settings.plot_width + 1);
self.derivative_data = data;
self.derivative_data = self.generate_plot_data(1, &resolution_iter);
debug_assert_eq!(self.derivative_data.len(), settings.plot_width + 1);
}
if self.nth_derviative && self.nth_derivative_data.is_none() {
let data: Vec<PlotPoint> = resolution_iter
.into_iter()
.map(|x| PlotPoint::new(x, self.function.get(self.curr_nth, x)))
.collect();
let data = self.generate_plot_data(self.curr_nth, &resolution_iter);
debug_assert_eq!(data.len(), settings.plot_width + 1);
self.nth_derivative_data = Some(data);
}