diff --git a/rtchallenge/examples/eoc4.rs b/rtchallenge/examples/eoc4.rs index 2df200c..43281c5 100644 --- a/rtchallenge/examples/eoc4.rs +++ b/rtchallenge/examples/eoc4.rs @@ -24,7 +24,7 @@ fn main() -> Result<()> { let w = 200; let h = w; let mut c = Canvas::new(w, h); - let t = Matrix4x4::translate(0., 0.75, 0.); + let t = Matrix4x4::translate(0., 0.4, 0.); let p = Tuple::point(0., 0., 0.); let rot_hour = Matrix4x4::rotation_z(-PI / 6.); @@ -33,11 +33,14 @@ fn main() -> Result<()> { let h = h as f32; let h_w = w / 2.0; let h_h = h / 2.0; + // The 'world' exists between -0.5 - 0.5 in X-Y plane. + // To convert to screen space, we translate by 0.5, scale to canvas size, + // and invert the Y-axis. + let world_to_screen = + Matrix4x4::scaling(w as f32, -h as f32, 1.0) * Matrix4x4::translate(0.5, -0.5, 0.); for _ in 0..12 { - let x = (h_w + h_w * p.x) as usize; - // Y-axis is 0 at the top, and h at the bottom, flip it. - let y = (h - (h_h + h_h * p.y)) as usize; - draw_dot(&mut c, x, y); + let canvas_pixel = world_to_screen * p; + draw_dot(&mut c, canvas_pixel.x as usize, canvas_pixel.y as usize); p = rot_hour * p; }