Implement most basic tuple

This commit is contained in:
Bill Thiede 2021-06-24 10:51:35 -07:00
parent a30a5a383c
commit 495c64249c
3 changed files with 51 additions and 0 deletions

1
rtchallenge/src/lib.rs Normal file
View File

@ -0,0 +1 @@
mod tuples;

3
rtchallenge/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

47
rtchallenge/src/tuples.rs Normal file
View File

@ -0,0 +1,47 @@
struct Tuple {
x: f32,
y: f32,
z: f32,
w: f32,
}
impl Tuple {
fn is_point(&self) -> bool {
self.w == 1.0
}
fn is_vector(&self) -> bool {
self.w == 0.0
}
}
fn tuple(x: f32, y: f32, z: f32, w: f32) -> Tuple {
Tuple { x, y, z, w }
}
#[cfg(test)]
mod tests {
use super::tuple;
#[test]
fn point() {
// A tuple with w = 1 is a point
let a = tuple(4.3, -4.2, 3.1, 1.0);
assert_eq!(a.x, 4.3);
assert_eq!(a.y, -4.2);
assert_eq!(a.z, 3.1);
assert_eq!(a.w, 1.0);
assert!(a.is_point());
assert!(!a.is_vector());
}
#[test]
fn vector() {
// A tuple with w = 0 is a point
let a = tuple(4.3, -4.2, 3.1, 0.0);
assert_eq!(a.x, 4.3);
assert_eq!(a.y, -4.2);
assert_eq!(a.z, 3.1);
assert_eq!(a.w, 0.0);
assert!(!a.is_point());
assert!(a.is_vector());
}
}