diff --git a/src/main.rs b/src/main.rs index 5640173..2fb15ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use core::time; mod app; +mod plotting_utils; mod widget_progress_bar; mod widget_main_chart; mod widget_text_output; diff --git a/src/plotting_utils.rs b/src/plotting_utils.rs new file mode 100644 index 0000000..8104375 --- /dev/null +++ b/src/plotting_utils.rs @@ -0,0 +1,35 @@ +pub fn sample_line(a: f64, b: f64, min: (f64, f64), max: (f64, f64), sample_rate: f64) -> Vec<(f64, f64)> { + let mut x = min.0; + let mut sampled_line: Vec<(f64, f64)> = vec![]; + + while x < max.0 { + sampled_line.push((x, a * x + b)); + x += sample_rate; + } + + sampled_line +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sample_horizontal_line() { + let line = sample_line(0.0, 42.0, (0.0, 0.0), (100.0, 100.0), 0.1); + + assert_eq!(line.len(), 1001, "not the good number of samples"); + assert_eq!((line[0].0.round(), line[0].1.round()), (0.0, 42.0)); + assert_eq!((line[10].0.round(), line[10].1.round()), (1.0, 42.0)); + assert_eq!((line[1000].0.round(), line[1000].1.round()), (100.0, 42.0)); + } + + #[test] + fn test_sample_vertical_line() { + let line = sample_line(std::f64::MAX, 0.0, (0.0, 0.0), (100.0, 100.0), 0.1); + + assert_eq!(line.len(), 1001, "not the good number of samples"); + assert_eq!(line[0], (0.0, 0.0)); + assert_eq!((line[1000].0.round(), line[1000].1.round()), (100.0, std::f64::INFINITY)); + } +} \ No newline at end of file diff --git a/src/widget_main_chart.rs b/src/widget_main_chart.rs index 18948fb..78d9c04 100644 --- a/src/widget_main_chart.rs +++ b/src/widget_main_chart.rs @@ -4,20 +4,9 @@ use termion::raw::RawTerminal; use std::io::Stdout; use tui::widgets::{Dataset, Marker, Chart, Block, Axis, Widget}; use tui::style::{Style, Color}; +use crate::plotting_utils::sample_line; -const SAMPLE_RATE: f64 = 0.1; - -fn sample_line(a: f64, b: f64, min: (f64, f64), max: (f64, f64), sample_rate: f64) -> Vec<(f64, f64)> { - let mut x = min.0; - let mut sampled_line: Vec<(f64, f64)> = vec![]; - - while x < max.0 { - x += sample_rate; - sampled_line.push((x, a * x + b)); - } - - sampled_line -} +const SAMPLE_RATE: f64 = 0.01; pub fn main_chart(frame: &mut Frame>>, regression: (f64, f64),