Implement end of chapter 1 exercises.

This commit is contained in:
Bill Thiede 2021-06-24 15:35:58 -07:00
parent df495feb57
commit 21ac03acfb

View File

@ -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;
}
}
}