summaryrefslogtreecommitdiff
path: root/src/lerp.rs
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-30 20:22:50 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-30 20:22:50 -0300
commit8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f (patch)
tree5e1f70f9fbad4fb25a0b9c66bd7c65c0823a3d70 /src/lerp.rs
parentfc22d14dfdb041797db66a9232b3beb06fada08d (diff)
downloadbezier-8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f.tar.gz
bezier-8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f.zip
Remove intermediate Lerp object
Now polynomials can be created directly from bezier components.
Diffstat (limited to 'src/lerp.rs')
-rw-r--r--src/lerp.rs87
1 files changed, 0 insertions, 87 deletions
diff --git a/src/lerp.rs b/src/lerp.rs
deleted file mode 100644
index b5b771d..0000000
--- a/src/lerp.rs
+++ /dev/null
@@ -1,87 +0,0 @@
-use crate::poly::Poly;
-use std::rc::Rc;
-
-#[derive(PartialEq, Debug)]
-pub enum Lerp {
- Node(Rc<Lerp>, Rc<Lerp>),
- Leaf(f32, f32),
- Just(f32),
-}
-
-impl Lerp {
- pub fn new(v: &Vec<f32>) -> Rc<Lerp> {
- match v.len() {
- 0 => Rc::new(Lerp::Just(0.0)),
- 1 => Rc::new(Lerp::Just(v[0])),
- 2 => Rc::new(Lerp::Leaf(v[0], v[1])),
- _ => {
- let mut lv = Vec::new();
- for i in 0..v.len() - 1 {
- lv.push(Rc::new(Lerp::Leaf(v[i], v[i + 1])));
- }
- while lv.len() > 1 {
- let mut nlv = Vec::new();
- for i in 0..lv.len() - 1 {
- nlv.push(Rc::new(Lerp::Node(lv[i].clone(), lv[i + 1].clone())));
- }
- lv = nlv;
- }
- lv[0].clone()
- }
- }
- }
-
- pub fn to_poly(&self) -> Poly {
- match self {
- Lerp::Just(a) => Poly::new(vec![*a]),
- Lerp::Leaf(a, b) => Poly::new(vec![*a, *b - *a]),
- Lerp::Node(a, b) => {
- let a = a.to_poly();
- let b = b.to_poly();
- let c = &b - &a;
- &a + &c.shift()
- }
- }
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn new2_test() {
- let a = Lerp::new(&vec![1.0, 2.0]);
- assert_eq!(a, Rc::new(Lerp::Leaf(1.0, 2.0)));
- }
-
- #[test]
- fn new3_test() {
- let a = Lerp::new(&vec![1.0, 2.0, 3.0]);
- assert_eq!(
- a,
- Rc::new(Lerp::Node(
- Rc::new(Lerp::Leaf(1.0, 2.0)),
- Rc::new(Lerp::Leaf(2.0, 3.0))
- ))
- );
- }
-
- #[test]
- fn new4_test() {
- let a = Lerp::new(&vec![1.0, 2.0, 3.0, 4.0]);
- assert_eq!(
- a,
- Rc::new(Lerp::Node(
- Rc::new(Lerp::Node(
- Rc::new(Lerp::Leaf(1.0, 2.0)),
- Rc::new(Lerp::Leaf(2.0, 3.0))
- )),
- Rc::new(Lerp::Node(
- Rc::new(Lerp::Leaf(2.0, 3.0)),
- Rc::new(Lerp::Leaf(3.0, 4.0))
- ))
- ))
- );
- }
-}