Implement point/vector constructors.

This commit is contained in:
Bill Thiede 2021-06-24 11:10:11 -07:00
parent 495c64249c
commit 3952a8ba83

View File

@ -1,3 +1,4 @@
#[derive(Debug, PartialEq)]
struct Tuple {
x: f32,
y: f32,
@ -15,15 +16,23 @@ impl Tuple {
}
}
fn point(x: f32, y: f32, z: f32) -> Tuple {
tuple(x, y, z, 1.0)
}
fn vector(x: f32, y: f32, z: f32) -> Tuple {
tuple(x, y, z, 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;
use super::{point, tuple, vector};
#[test]
fn point() {
fn is_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);
@ -34,7 +43,7 @@ mod tests {
assert!(!a.is_vector());
}
#[test]
fn vector() {
fn is_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);
@ -44,4 +53,12 @@ mod tests {
assert!(!a.is_point());
assert!(a.is_vector());
}
#[test]
fn point_tuple() {
assert_eq!(point(4., -4., 3.), tuple(4., -4., 3., 1.))
}
#[test]
fn vector_tuple() {
assert_eq!(vector(4., -4., 3.), tuple(4., -4., 3., 0.))
}
}