From 79554285331846db8a1d06e2570d5acbb2f2690e Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sun, 21 Jan 2024 17:25:43 +0100 Subject: implement dynamic graph removal and pushing --- src/ui/graph.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/ui') diff --git a/src/ui/graph.rs b/src/ui/graph.rs index 83d90f8..14f0c7d 100644 --- a/src/ui/graph.rs +++ b/src/ui/graph.rs @@ -36,6 +36,7 @@ impl GraphCanvas { pub fn push(&mut self, f: &str) { if let Ok(f) = ExpressionFunction::from_string(f.to_string(), self.ctx.operations()) { + self.ctx.set_function(f.name(), std::sync::Arc::new(f.clone())); self.funcs.lock().unwrap().push(Some(FunctionCache::new(f))); } else { self.funcs.lock().unwrap().push(None); @@ -43,15 +44,28 @@ impl GraphCanvas { } pub fn set(&mut self, i: usize, f: &str) { - if let Ok(f) = ExpressionFunction::from_string(f.to_string(), self.ctx.operations()) { - self.funcs.lock().unwrap()[i] = Some(FunctionCache::new(f)); - } else { - self.funcs.lock().unwrap().push(None); + if let Ok(mut vf) = self.funcs.lock() { + if let Some(last_f) = &vf[i] { + self.ctx.remove_function(last_f.name()); + } + + if let Ok(f) = ExpressionFunction::from_string(f.to_string(), self.ctx.operations()) { + self.ctx.set_function(f.name(), std::sync::Arc::new(f.clone())); + vf[i] = Some(FunctionCache::new(f)); + } else { + vf[i] = None; + } + } } pub fn remove(&mut self, i: usize) { - self.funcs.lock().unwrap().remove(i); + if let Ok(mut vf) = self.funcs.lock() { + if let Some(f) = &vf[i] { + self.ctx.remove_function(f.name()); + } + vf.remove(i); + } } } @@ -220,10 +234,11 @@ impl canvas::Program for GraphCanvas { { let mut y1 = func.eval(Complex::new(rect.x as f64, 0.0), &self.ctx).real as f32; for x in (rect.x as i64..(rect.x + rect.width) as i64).step_by(step) { - for d in 0..10 { - let d = d as f32 / 10.0; + let precision = state.scale as i64; + for d in 0..precision { + let d = d as f32 / (precision as f32); let x1 = x as f32 + d * step as f32; - let x2 = x as f32 + (d + 0.1) * step as f32; + let x2 = x as f32 + (d + (1.0 / precision as f32)) * step as f32; let y2 = func.eval(Complex::new(x2 as f64, 0.0), &self.ctx).real as f32; if y1.is_finite() && !y1.is_nan() && y2.is_finite() && !y2.is_nan() { let line = -- cgit v1.2.3-70-g09d2