diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 31 |
1 files changed, 27 insertions, 4 deletions
@@ -49,7 +49,30 @@ impl Bezier { self.degree -= 1; } - pub fn show_x(&self) -> String { + fn closer(&self, x: f32, y: f32) -> usize { + let mut best = 10000.0; + let mut best_index = 0; + for i in 0..self.degree { + let candidate = ((x - self.vx[i]).powi(2) + (y - self.vy[i]).powi(2)).sqrt(); + if candidate < best { + best = candidate; + best_index = i; + } + } + best_index + } + + pub fn grab_closer(&mut self, x: f32, y: f32) { + let closer = self.closer(x, y); + self.vx[closer] = x; + self.vy[closer] = y; + self.px = to_poly(&self.vx); + self.py = to_poly(&self.vy); + self.dpx = self.px.deriv(); + self.dpy = self.py.deriv(); + } + + fn show_x(&self) -> String { let mut s = String::new(); for i in 0..self.degree { s.push_str(&self.vx[i].to_string()); @@ -60,7 +83,7 @@ impl Bezier { s } - pub fn show_y(&self) -> String { + fn show_y(&self) -> String { let mut s = String::new(); for i in 0..self.degree { s.push_str(&self.vy[i].to_string()); @@ -96,8 +119,8 @@ impl Bezier { float dpx[{dsx}] = float[{dsx}]({dpx}); float dpy[{dsy}] = float[{dsy}]({dpy}); - float thr = 32; - float step_size = 10; + float thr = 16; + float step_size = 5; float inf = 1000000; float eval_px(float t) {{ |