summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-10 19:54:34 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-10 19:54:34 -0300
commit7fa34698ba8ee80227fa0b8eaab7a7382a5cf465 (patch)
tree2a2b8bbfd2de5c93744629e0a16bc82f53e63273
parent6d356f0c681cb2bb0f26d848ed467f65c49769a5 (diff)
downloadbezier-7fa34698ba8ee80227fa0b8eaab7a7382a5cf465.tar.gz
bezier-7fa34698ba8ee80227fa0b8eaab7a7382a5cf465.zip
Separate project into modules
-rw-r--r--Cargo.lock18
-rw-r--r--Cargo.toml2
-rw-r--r--src/lerp.rs33
-rw-r--r--src/main.rs32
-rw-r--r--src/poly.rs (renamed from src/lib.rs)39
5 files changed, 63 insertions, 61 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6e75461..86f5dd3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,13 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
+name = "bezier"
+version = "0.1.0"
+dependencies = [
+ "sdl2",
+]
+
+[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -20,16 +27,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
-version = "0.2.81"
+version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
-
-[[package]]
-name = "poly"
-version = "0.1.0"
-dependencies = [
- "sdl2",
-]
+checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929"
[[package]]
name = "sdl2"
diff --git a/Cargo.toml b/Cargo.toml
index cad75f6..6eed337 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[package]
-name = "poly"
+name = "bezier"
version = "0.1.0"
authors = ["Juan Manuel Tomás <jtomas1815@gmail.com>"]
edition = "2018"
diff --git a/src/lerp.rs b/src/lerp.rs
new file mode 100644
index 0000000..7c4994c
--- /dev/null
+++ b/src/lerp.rs
@@ -0,0 +1,33 @@
+use crate::poly::{self, Number, Poly};
+
+pub enum Lerp {
+ Node(Box<Lerp>, Box<Lerp>),
+ Leaf(Number, Number)
+}
+
+impl Lerp {
+ pub fn new(v: Vec<Number>) -> Box<Lerp> {
+ Lerp::new_s(&v[..])
+ }
+
+ fn new_s(v: &[Number]) -> Box<Lerp> {
+ match v.len() {
+ 0 => Box::new(Lerp::Leaf(0.0, 0.0)),
+ 1 => Box::new(Lerp::Leaf(v[0], v[0])),
+ 2 => Box::new(Lerp::Leaf(v[0], v[1])),
+ _ => Box::new(Lerp::Node(Lerp::new_s(&v[0..v.len() - 1]), Lerp::new_s(&v[1..v.len()])))
+ }
+ }
+}
+
+pub fn lp(l: Box<Lerp>) -> Poly {
+ match *l {
+ Lerp::Leaf(a, b) => vec![a, b - a],
+ Lerp::Node(a, b) => {
+ let a = lp(a);
+ let b = lp(b);
+ let c = poly::sub(&b, &a);
+ poly::skewed_sum(a, c)
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 8831976..8c23d3e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,28 +1,22 @@
-use poly::Poly;
-use poly::Number;
-use poly::Lerp;
+mod poly;
+mod lerp;
-use sdl2::pixels::Color;
-use sdl2::event::Event;
-use sdl2::keyboard::Keycode;
-use sdl2::rect::Rect;
+use crate::poly::Number;
+use crate::lerp::Lerp;
use std::thread;
use std::time::Duration;
-fn eval_poly(p: &Poly, t: Number) -> Number {
- let mut r = 0.0;
- for i in 0..p.len() {
- r += p[i] * t.powi(i as i32);
- }
- r
-}
+use sdl2::pixels::Color;
+use sdl2::event::Event;
+use sdl2::keyboard::Keycode;
+use sdl2::rect::Rect;
fn main() {
let a = Lerp::new(vec![100.0, 200.0, 300.0, 500.0]);
let b = Lerp::new(vec![400.0, 140.0, 500.0, 300.0]);
- let pa = poly::lp(a);
- let pb = poly::lp(b);
+ let pa = lerp::lp(a);
+ let pb = lerp::lp(b);
let p = poly::gcd(&pa, &pb);
let sdl_context = sdl2::init().unwrap();
@@ -50,9 +44,9 @@ fn main() {
}
for t in 0..800 {
- let x = eval_poly(&pa, t as Number / 800.0);
- let y = eval_poly(&pb, t as Number / 800.0);
- let z = eval_poly(&p, t as Number / 800.0);
+ let x = poly::eval_poly(&pa, t as Number / 800.0);
+ let y = poly::eval_poly(&pb, t as Number / 800.0);
+ let z = poly::eval_poly(&p, t as Number / 800.0);
canvas.set_draw_color(Color::RGB(180, 20, 20));
canvas.fill_rect(Rect::new(t, x as i32, 5, 5)).unwrap();
canvas.set_draw_color(Color::RGB(20, 180, 20));
diff --git a/src/lib.rs b/src/poly.rs
index c70a40f..2128d38 100644
--- a/src/lib.rs
+++ b/src/poly.rs
@@ -4,40 +4,15 @@ pub type Number = f32;
pub type Poly = Vec<Number>;
-#[derive(Debug)]
-pub enum Lerp {
- Node(Box<Lerp>, Box<Lerp>),
- Leaf(Number, Number)
-}
-
-impl Lerp {
- pub fn new(v: Vec<Number>) -> Box<Lerp> {
- Lerp::new_s(&v[..])
- }
-
- fn new_s(v: &[Number]) -> Box<Lerp> {
- match v.len() {
- 0 => Box::new(Lerp::Leaf(0.0, 0.0)),
- 1 => Box::new(Lerp::Leaf(v[0], v[0])),
- 2 => Box::new(Lerp::Leaf(v[0], v[1])),
- _ => Box::new(Lerp::Node(Lerp::new_s(&v[0..v.len() - 1]), Lerp::new_s(&v[1..v.len()])))
- }
- }
-}
-
-pub fn lp(l: Box<Lerp>) -> Poly {
- match *l {
- Lerp::Leaf(a, b) => vec![a, b - a],
- Lerp::Node(a, b) => {
- let a = lp(a);
- let b = lp(b);
- let c = sub(&b, &a);
- skewed_sum(a, c)
- }
+pub fn eval_poly(p: &Poly, t: Number) -> Number {
+ let mut r = 0.0;
+ for i in 0..p.len() {
+ r += p[i] * t.powi(i as i32);
}
+ r
}
-fn sub(a: &Poly, b: &Poly) -> Poly {
+pub fn sub(a: &Poly, b: &Poly) -> Poly {
let mut r = a.clone();
for i in 0..r.len() {
r[i] -= b[i];
@@ -45,7 +20,7 @@ fn sub(a: &Poly, b: &Poly) -> Poly {
r
}
-fn skewed_sum(a: Poly, b: Poly) -> Poly {
+pub fn skewed_sum(a: Poly, b: Poly) -> Poly {
let mut r = a.clone();
for i in 0..r.len() - 1 {
r[i + 1] += b[i];