some preperations

This commit is contained in:
Simon Gardling 2022-02-24 23:02:55 -05:00
parent 9f57f8fa0d
commit 50c033a266
3 changed files with 52 additions and 21 deletions

View File

@ -1,6 +1,5 @@
## TODO: ## TODO:
1. Port to [egui](https://github.com/emilk/egui) 1. Proper support for dynamic chart display size.
- Proper support for dynamic chart display size. 2. Fix (very rare) crashes in parsing strings (`TextDecoder.decode: Decoding failed.`)
- Fix (rare) crashes in parsing strings (`TextDecoder.decode: Decoding failed.`) 3. Multiple functions in one graph.
2. Multiple functions in one graph. 4. Non `y=` functions.
3. Non `y=` functions.

View File

@ -12,18 +12,23 @@ pub struct ChartManager {
function: Function, function: Function,
min_x: f64, min_x: f64,
max_x: f64, max_x: f64,
min_x_back: f64,
max_x_back: f64,
num_interval: usize, num_interval: usize,
resolution: usize, resolution: usize,
} }
impl ChartManager { impl ChartManager {
pub fn new( pub fn new(
func_str: String, min_x: f64, max_x: f64, num_interval: usize, resolution: usize, func_str: String, min_x: f64, max_x: f64, min_x_back: f64, max_x_back: f64,
num_interval: usize, resolution: usize,
) -> Self { ) -> Self {
Self { Self {
function: Function::from_string(func_str), function: Function::from_string(func_str),
min_x, min_x,
max_x, max_x,
min_x_back,
max_x_back,
num_interval, num_interval,
resolution, resolution,
} }
@ -31,9 +36,9 @@ impl ChartManager {
#[inline] #[inline]
pub fn draw_back(&mut self) -> Vec<(f64, f64)> { pub fn draw_back(&mut self) -> Vec<(f64, f64)> {
let absrange = (self.max_x - self.min_x).abs(); let absrange = (self.max_x_back - self.min_x_back).abs();
let output: Vec<(f64, f64)> = (1..=self.resolution) let output: Vec<(f64, f64)> = (1..=self.resolution)
.map(|x| ((x as f64 / self.resolution as f64) * absrange) + self.min_x) .map(|x| ((x as f64 / self.resolution as f64) * absrange) + self.min_x_back)
.map(|x| (x, self.function.run(x))) .map(|x| (x, self.function.run(x)))
.collect(); .collect();
output output
@ -49,15 +54,19 @@ impl ChartManager {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn update( pub fn update(
&mut self, func_str_new: String, min_x: f64, max_x: f64, num_interval: usize, &mut self, func_str_new: String, min_x: f64, max_x: f64, min_x_back: f64, max_x_back: f64,
resolution: usize, num_interval: usize, resolution: usize,
) -> UpdateType { ) -> UpdateType {
let func_str: String = add_asterisks(func_str_new); let func_str: String = add_asterisks(func_str_new);
let update_func: bool = !self.function.str_compare(func_str.clone()); let update_func: bool = !self.function.str_compare(func_str.clone());
let update_back = update_func | (min_x != self.min_x) | (max_x != self.max_x); let update_back =
let update_front = update_func | (min_x_back != self.min_x_back) | (max_x_back != self.max_x_back);
update_back | (self.resolution != resolution) | (num_interval != self.num_interval); let update_front = update_func
| (min_x != self.min_x)
| (max_x != self.max_x)
| (self.resolution != resolution)
| (num_interval != self.num_interval);
if update_func { if update_func {
self.function = Function::from_string(func_str); self.function = Function::from_string(func_str);
@ -65,6 +74,8 @@ impl ChartManager {
self.min_x = min_x; self.min_x = min_x;
self.max_x = max_x; self.max_x = max_x;
self.min_x_back = min_x_back;
self.max_x_back = max_x_back;
self.num_interval = num_interval; self.num_interval = num_interval;
self.resolution = resolution; self.resolution = resolution;

View File

@ -11,10 +11,21 @@ use git_version::git_version;
// Grabs git version on compile time // Grabs git version on compile time
const GIT_VERSION: &str = git_version!(); const GIT_VERSION: &str = git_version!();
// Sets some hard-coded limits to the application
const NUM_INTERVAL_RANGE: RangeInclusive<usize> = 10..=10000;
const MIN_X_TOTAL: f64 = -1000.0;
const MAX_X_TOTAL: f64 = 1000.0;
const X_RANGE: RangeInclusive<f64> = MIN_X_TOTAL..=MAX_X_TOTAL;
pub struct MathApp { pub struct MathApp {
func_str: String, func_str: String,
min_x: f64, min_x: f64,
max_x: f64, max_x: f64,
// Currently really unused. But once fully implemented it will represent the full graph's min_x and max_x, being seperate from min_x and max_x for the intergral.
min_x_graph: f64,
max_x_graph: f64,
num_interval: usize, num_interval: usize,
resolution: usize, resolution: usize,
chart_manager: ChartManager, chart_manager: ChartManager,
@ -34,12 +45,16 @@ impl Default for MathApp {
func_str: def_func.clone(), func_str: def_func.clone(),
min_x: def_min_x, min_x: def_min_x,
max_x: def_max_x, max_x: def_max_x,
min_x_graph: def_min_x,
max_x_graph: def_max_x,
num_interval: def_interval, num_interval: def_interval,
resolution: def_resolution, resolution: def_resolution,
chart_manager: ChartManager::new( chart_manager: ChartManager::new(
def_func, def_func,
def_min_x, def_min_x,
def_max_x, def_max_x,
def_min_x,
def_max_x,
def_interval, def_interval,
def_resolution, def_resolution,
), ),
@ -86,12 +101,6 @@ impl MathApp {
} }
} }
// Sets some hard-coded limits to the application
const NUM_INTERVAL_RANGE: RangeInclusive<usize> = 10..=10000;
const MIN_X_TOTAL: f32 = -1000.0;
const MAX_X_TOTAL: f32 = 1000.0;
const X_RANGE: RangeInclusive<f64> = MIN_X_TOTAL as f64..=MAX_X_TOTAL as f64;
impl epi::App for MathApp { impl epi::App for MathApp {
fn name(&self) -> &str { "Integral Demonstration" } fn name(&self) -> &str { "Integral Demonstration" }
@ -107,6 +116,8 @@ impl epi::App for MathApp {
func_str, func_str,
min_x, min_x,
max_x, max_x,
min_x_graph,
max_x_graph,
num_interval, num_interval,
resolution, resolution,
chart_manager, chart_manager,
@ -162,6 +173,8 @@ impl epi::App for MathApp {
*min_x = -10.0; *min_x = -10.0;
*max_x = 10.0; *max_x = 10.0;
} }
*min_x_graph = *min_x;
*max_x_graph = *max_x;
} }
ui.add(egui::Slider::new(num_interval, NUM_INTERVAL_RANGE).text("Interval")); ui.add(egui::Slider::new(num_interval, NUM_INTERVAL_RANGE).text("Interval"));
@ -195,8 +208,15 @@ impl epi::App for MathApp {
}); });
if parse_error.is_empty() { if parse_error.is_empty() {
let do_update = let do_update = chart_manager.update(
chart_manager.update(func_str.clone(), *min_x, *max_x, *num_interval, *resolution); func_str.clone(),
*min_x,
*max_x,
*min_x_graph,
*max_x_graph,
*num_interval,
*resolution,
);
// Invalidates caches according to what settings were changed // Invalidates caches according to what settings were changed
match do_update { match do_update {
@ -224,6 +244,7 @@ impl epi::App for MathApp {
Plot::new("plot") Plot::new("plot")
.view_aspect(1.0) .view_aspect(1.0)
.data_aspect(1.0)
.include_y(0) .include_y(0)
.show(ui, |plot_ui| { .show(ui, |plot_ui| {
plot_ui.line(curve); plot_ui.line(curve);