From 71fc73bf1a3149ca32769aa55f1656917debb39f Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Sat, 1 Jan 2022 16:30:51 -0700 Subject: [PATCH] tuple::new can take any type of number --- .vscode/launch.json | 45 +++++++++++++++++++++++++++++++++++++++++ Cargo.lock | 1 + features/Cargo.lock | 1 + features/Cargo.toml | 1 + features/src/lib.rs | 1 + features/src/structs.rs | 18 ++++++++++++----- src/main.rs | 2 +- 7 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..dad713c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'ray-tracer'", + "cargo": { + "args": [ + "build", + "--bin=ray-tracer", + "--package=ray-tracer" + ], + "filter": { + "name": "ray-tracer", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'ray-tracer'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=ray-tracer", + "--package=ray-tracer" + ], + "filter": { + "name": "ray-tracer", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index f4cefa4..a08d573 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,6 +22,7 @@ name = "features" version = "0.1.0" dependencies = [ "approx", + "num-traits", ] [[package]] diff --git a/features/Cargo.lock b/features/Cargo.lock index ad1adb1..8764178 100644 --- a/features/Cargo.lock +++ b/features/Cargo.lock @@ -22,6 +22,7 @@ name = "features" version = "0.1.0" dependencies = [ "approx", + "num-traits", ] [[package]] diff --git a/features/Cargo.toml b/features/Cargo.toml index b3ff419..1f7e75c 100644 --- a/features/Cargo.toml +++ b/features/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" [dependencies] approx = "0.5" +num-traits = "0.2" \ No newline at end of file diff --git a/features/src/lib.rs b/features/src/lib.rs index 49d4c5e..4ed24fc 100644 --- a/features/src/lib.rs +++ b/features/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] extern crate approx; +extern crate num_traits; pub mod structs; pub mod color; diff --git a/features/src/structs.rs b/features/src/structs.rs index 07ad068..aefbde5 100644 --- a/features/src/structs.rs +++ b/features/src/structs.rs @@ -10,12 +10,12 @@ pub struct Tuple { } impl Tuple { - pub fn new(x: f32, y: f32, z: f32, w: f32) -> Tuple { + pub fn new(x: T, y: T, z: T, w: T) -> Tuple { Tuple { - x, - y, - z, - w, + x: num_traits::cast(x).unwrap(), + y: num_traits::cast(y).unwrap(), + z: num_traits::cast(z).unwrap(), + w: num_traits::cast(w).unwrap(), } } @@ -424,4 +424,12 @@ mod tests { let b_cross_a = Tuple::vector(1.0, -2.0, 1.0); assert_eq!(b_cross_a, b.cross(&a)); } + + #[test] + fn works_with_i32() { + let a = Tuple::new(1, 2, 3); + assert_eq!(1.0, a.x()); + assert_eq!(2.0, a.y()); + assert_eq!(3.0, a.z()); + } } diff --git a/src/main.rs b/src/main.rs index ee7a673..232027c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -175,7 +175,7 @@ fn clock() { let mut canvas = Canvas::new(1024, 1024); let color = Color::new(1.0, 0.0, 0.0); let middle = 1024.0 / 2.0; - canvas.write_pixel(middle as usize, middle as usize, Color::new(0.0, 1.0, 0.0)); + let middle_point = &Tuple::point_zero() * &Matrix::translation(middle, middle, 0.0); draw_cross(&mut canvas, &middle_point, Color::new(0.0, 1.0, 0.0));