diff --git a/rtchallenge/Cargo.lock b/rtchallenge/Cargo.lock index c47050a..d19b78a 100644 --- a/rtchallenge/Cargo.lock +++ b/rtchallenge/Cargo.lock @@ -1,11 +1,60 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + +[[package]] +name = "anyhow" +version = "1.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15af2628f6890fe2609a3b91bef4c83450512802e59489f9c1cb1fa5df064a61" + [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crc32fast" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "deflate" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" +dependencies = [ + "adler32", + "byteorder", +] + [[package]] name = "float-cmp" version = "0.8.0" @@ -15,6 +64,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + [[package]] name = "num-traits" version = "0.2.14" @@ -24,9 +82,79 @@ dependencies = [ "autocfg", ] +[[package]] +name = "png" +version = "0.16.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" +dependencies = [ + "bitflags", + "crc32fast", + "deflate", + "miniz_oxide", +] + +[[package]] +name = "proc-macro2" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rtchallenge" version = "0.1.0" dependencies = [ + "anyhow", "float-cmp", + "png", + "thiserror", ] + +[[package]] +name = "syn" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "thiserror" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa6f76457f59514c7eeb4e59d891395fab0b2fd1d40723ae737d64153392e9c6" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a36768c0fbf1bb15eca10defa29526bda730a2376c2ab4393ccfa16fb1a318d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/rtchallenge/Cargo.toml b/rtchallenge/Cargo.toml index 5fb80a0..162f92a 100644 --- a/rtchallenge/Cargo.toml +++ b/rtchallenge/Cargo.toml @@ -7,4 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.41" float-cmp = "0.8.0" +png = "0.16.8" +thiserror = "1.0.25" diff --git a/rtchallenge/examples/eoc2.rs b/rtchallenge/examples/eoc2.rs new file mode 100644 index 0000000..885af1f --- /dev/null +++ b/rtchallenge/examples/eoc2.rs @@ -0,0 +1,71 @@ +use anyhow::{bail, Result}; + +use rtchallenge::{ + canvas::{canvas, Canvas}, + tuples::{color, point, vector, Tuple}, +}; + +#[derive(Debug)] +struct Environment { + gravity: Tuple, + wind: Tuple, +} +#[derive(Debug)] +struct Projectile { + position: Tuple, + velocity: Tuple, +} + +fn tick(env: &Environment, proj: &Projectile) -> Projectile { + let position = proj.position + proj.velocity; + let velocity = proj.velocity + env.gravity + env.wind; + Projectile { position, velocity } +} + +fn draw_dot(c: &mut Canvas, x: usize, y: usize) { + let red = color(1., 0., 0.); + c.set(x.saturating_sub(1), y.saturating_sub(1), red); + c.set(x, y.saturating_sub(1), red); + c.set(x + 1, y.saturating_sub(1), red); + c.set(x.saturating_sub(1), y, red); + c.set(x, y, red); + c.set(x + 1, y, red); + c.set(x.saturating_sub(1), y + 1, red); + c.set(x, y + 1, red); + c.set(x + 1, y + 1, red); +} +fn main() -> Result<()> { + let mut p = Projectile { + position: point(0., 1., 0.), + velocity: 10. * vector(1., 2., 0.).normalize(), + }; + let e = Environment { + gravity: vector(0., -0.1, 0.), + wind: vector(-0.01, 0., 0.), + }; + + let w = 800; + let h = 600; + let mut c = canvas(w, h); + let mut i = 0; + let w = w as f32; + let h = h as f32; + while p.position.y > 0. { + p = tick(&e, &p); + println!("tick {}: proj {:?}", i, p); + let x = p.position.x; + let y = p.position.y; + if x > 0. && x < w && y > 0. && y < h { + let x = x as usize; + let y = (h - y) as usize; + draw_dot(&mut c, x, y) + } + i += 1; + if i > 1000 { + bail!("too many iterations"); + } + } + + c.write_to_file("/tmp/output.png")?; + Ok(()) +}