From 21ac03acfb1a62538655fdd81f8850b872535e0e Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Thu, 24 Jun 2021 15:35:58 -0700 Subject: [PATCH] Implement end of chapter 1 exercises. --- rtchallenge/examples/eoc1.rs | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rtchallenge/examples/eoc1.rs diff --git a/rtchallenge/examples/eoc1.rs b/rtchallenge/examples/eoc1.rs new file mode 100644 index 0000000..c8f0110 --- /dev/null +++ b/rtchallenge/examples/eoc1.rs @@ -0,0 +1,39 @@ +use rtchallenge::tuples::{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 main() { + let mut p = Projectile { + position: point(0., 1., 0.), + velocity: 4. * vector(1., 1., 0.).normalize(), + }; + let e = Environment { + gravity: vector(0., -0.1, 0.).normalize(), + wind: vector(-0.01, 0., 0.), + }; + + let mut i = 0; + while p.position.y > 0. { + p = tick(&e, &p); + println!("tick {}: position {:?}", i, p.position); + i += 1; + if i > 100 { + eprintln!("too many iterations"); + return; + } + } +}