summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-31 02:41:18 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-31 02:41:18 -0300
commitd6341b9195553af6dfb8bfc9d94c19e6cc000247 (patch)
tree546b204d15714993689c85136a3ffcaac04087f2
parentc0089dd0567b8d6538360a21f042cbb0913fff99 (diff)
downloadbezier-d6341b9195553af6dfb8bfc9d94c19e6cc000247.tar.gz
bezier-d6341b9195553af6dfb8bfc9d94c19e6cc000247.zip
Change the curve style
-rw-r--r--src/lib.rs31
-rw-r--r--src/main.rs7
2 files changed, 34 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 4662f08..dd28124 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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) {{
diff --git a/src/main.rs b/src/main.rs
index e6f3720..add9d00 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -63,6 +63,7 @@ fn main() {
'running: loop {
let x = event_pump.mouse_state().x();
let y = event_pump.mouse_state().y();
+ let left_click = event_pump.mouse_state().left();
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
@@ -76,6 +77,12 @@ fn main() {
} => {
curve.push(x, window_h as i32 - y);
curve.draw();
+ },
+ Event::MouseMotion { .. } => {
+ if left_click {
+ curve.grab_closer(x as f32, (window_h as i32 - y) as f32);
+ curve.draw();
+ }
}
_ => {}
}