summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-07 20:14:15 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2021-01-07 20:14:15 -0300
commit9724f0d0fe318615e4ed79b1600dd4557a2378eb (patch)
tree11a208fe9eb427774d6dd2e529d69571a2ea6292
parentad4c09192e6144e3e914748f9ce4edf371cf75b8 (diff)
downloadbezier-9724f0d0fe318615e4ed79b1600dd4557a2378eb.tar.gz
bezier-9724f0d0fe318615e4ed79b1600dd4557a2378eb.zip
Visualize polynomials using sdl
-rw-r--r--Cargo.lock56
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs64
3 files changed, 118 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b1c5475..6e75461 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,5 +1,61 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.81"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
+
+[[package]]
name = "poly"
version = "0.1.0"
+dependencies = [
+ "sdl2",
+]
+
+[[package]]
+name = "sdl2"
+version = "0.34.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fcbb85f4211627a7291c83434d6bbfa723e28dcaa53c7606087e3c61929e4b9c"
+dependencies = [
+ "bitflags",
+ "lazy_static",
+ "libc",
+ "sdl2-sys",
+]
+
+[[package]]
+name = "sdl2-sys"
+version = "0.34.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28d81feded049b9c14eceb4a4f6d596a98cebbd59abdba949c5552a015466d33"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "version-compare",
+]
+
+[[package]]
+name = "version-compare"
+version = "0.0.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1"
diff --git a/Cargo.toml b/Cargo.toml
index ab64b5d..cad75f6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+sdl2 = "0.34.3"
diff --git a/src/main.rs b/src/main.rs
index 1e4f704..13f13da 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,63 @@
+use poly::Poly;
+use poly::Number;
+use poly::Lerp;
+
+use sdl2::pixels::Color;
+use sdl2::event::Event;
+use sdl2::keyboard::Keycode;
+use sdl2::rect::Rect;
+
+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
+}
+
fn main() {
- let a = poly::Lerp::new(vec![1.0, 5.0, 8.0, 3.0, 10.0]);
- println!("{:?}", a);
- println!("{:?}", poly::lp(a));
+ 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 sdl_context = sdl2::init().unwrap();
+ let video_subsystem = sdl_context.video().unwrap();
+
+ let window = video_subsystem.window("rust-sdl2 demo", 800, 600)
+ .position_centered()
+ .build()
+ .unwrap();
+
+ let mut canvas = window.into_canvas().build().unwrap();
+
+ let mut event_pump = sdl_context.event_pump().unwrap();
+ 'running: loop {
+ canvas.set_draw_color(Color::RGB(20, 20, 20));
+ canvas.clear();
+ for event in event_pump.poll_iter() {
+ match event {
+ Event::Quit {..} |
+ Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
+ break 'running
+ },
+ _ => {}
+ }
+ }
+
+ 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);
+ 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));
+ canvas.fill_rect(Rect::new(t, y as i32, 5, 5)).unwrap();
+ }
+
+ canvas.present();
+ thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
+ }
}