summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-17 20:25:58 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-17 20:25:58 -0300
commit70b5188de306b70cdf8a397dd4e0ee31f07050c8 (patch)
tree3721070393126b79a3658fe8a89f1fa98a84db44 /src/lib.rs
parent18efe0d28ebcd936ea50275a351ef1c30253ea1c (diff)
downloadbezier-70b5188de306b70cdf8a397dd4e0ee31f07050c8.tar.gz
bezier-70b5188de306b70cdf8a397dd4e0ee31f07050c8.zip
Fix curve overextending past last control point
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..cadef3c
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,62 @@
+mod lerp;
+
+use lerp::Lerp;
+use lerp::poly::Poly;
+
+pub struct Bezier {
+ pub vx: Vec<f32>,
+ pub vy: Vec<f32>,
+ pub px: Poly,
+ pub py: Poly,
+ pub degree: usize
+}
+
+impl Bezier {
+ pub fn new() -> Bezier {
+ Bezier {
+ vx: Vec::<f32>::new(),
+ vy: Vec::<f32>::new(),
+ px: Poly::new(vec![]),
+ py: 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.clone()).to_poly();
+ self.py = Lerp::new(self.vy.clone()).to_poly();
+ self.degree += 1;
+ }
+
+ pub fn remove(&mut self, index: usize) {
+ self.vx.remove(index);
+ self.vy.remove(index);
+ self.px = Lerp::new(self.vx.clone()).to_poly();
+ self.py = Lerp::new(self.vy.clone()).to_poly();
+ 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
+ }
+}