aboutsummaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-01-21 17:25:43 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2024-01-21 17:25:43 +0100
commit79554285331846db8a1d06e2570d5acbb2f2690e (patch)
treecf590a0caf22eabf1809a8d43992ac11128b75a0 /src/ui
parentb9b32972afda261020dd207b4ea2b44b7b697b83 (diff)
implement dynamic graph removal and pushing
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/graph.rs31
1 files changed, 23 insertions, 8 deletions
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<Message, Renderer> 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 =