eoc5: implement suggestiong at end of chapter 5.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bill Thiede 2021-07-16 22:25:48 -07:00
parent cb1b3ec801
commit 4bb6a72e4b

View File

@ -0,0 +1,41 @@
use core::f32;
use anyhow::Result;
use rtchallenge::{
canvas::Canvas,
rays::Ray,
spheres::{intersect, Sphere},
tuples::{Color, Tuple},
};
fn main() -> Result<()> {
let w = 200;
let h = w;
let mut c = Canvas::new(w, h);
let ray_origin = Tuple::point(0., 0., -5.);
let wall_z = 10.;
let wall_size = 7.;
let pixel_size = wall_size / w as f32;
let half = wall_size / 2.;
let color = Color::new(1., 0., 0.);
let shape = Sphere::default();
for y in 0..h {
let world_y = half - pixel_size * y as f32;
for x in 0..w {
let world_x = -half + pixel_size * x as f32;
let position = Tuple::point(world_x, world_y, wall_z);
let r = Ray::new(ray_origin, (position - ray_origin).normalize());
let xs = intersect(&shape, &r);
if xs.hit().is_some() {
c.set(x, y, color);
}
}
}
let path = "/tmp/eoc5.png";
println!("saving output to {}", path);
c.write_to_file(path)?;
Ok(())
}