summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: d6a1374e56f48b278f5c80154696280f0c17724c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
mod lerp;
mod poly;

use lerp::Lerp;
use poly::Poly;

pub struct Bezier {
    pub degree: usize,
    pub vx: Vec<f32>,
    pub vy: Vec<f32>,
    pub px: Poly,
    pub py: Poly,
    pub dpx: Poly,
    pub dpy: Poly,
}

impl Bezier {
    pub fn new() -> Bezier {
        Bezier {
            vx: Vec::<f32>::new(),
            vy: Vec::<f32>::new(),
            px: Poly::new(vec![]),
            py: Poly::new(vec![]),
            dpy: Poly::new(vec![]),
            dpx: Poly::new(vec![]),
            degree: 0,
        }
    }

    pub fn push(&mut self, x: i32, y: i32) {
        self.vx.push(x as f32);
        self.vy.push(y as f32);
        self.px = Lerp::new(&self.vx).to_poly();
        self.py = Lerp::new(&self.vy).to_poly();
        self.dpx = self.px.deriv();
        self.dpy = self.py.deriv();
        self.degree += 1;
    }

    pub fn remove(&mut self, index: usize) {
        self.vx.remove(index);
        self.vy.remove(index);
        self.px = Lerp::new(&self.vx).to_poly();
        self.py = Lerp::new(&self.vy).to_poly();
        self.dpx = self.px.deriv();
        self.dpy = self.py.deriv();
        self.degree -= 1;
    }

    pub fn show_x(&self) -> String {
        let mut s = String::new();
        for i in 0..self.degree {
            s.push_str(&self.vx[i].to_string());
            if i < self.degree - 1 {
                s.push_str(", ");
            }
        }
        s
    }

    pub fn show_y(&self) -> String {
        let mut s = String::new();
        for i in 0..self.degree {
            s.push_str(&self.vy[i].to_string());
            if i < self.degree - 1 {
                s.push_str(", ");
            }
        }
        s
    }
}