diff --git a/rtchallenge/src/lib.rs b/rtchallenge/src/lib.rs index 58b9c78..303381b 100644 --- a/rtchallenge/src/lib.rs +++ b/rtchallenge/src/lib.rs @@ -1,3 +1,6 @@ pub mod canvas; pub mod matrices; pub mod tuples; + +/// Value considered close enough for PartialEq implementations. +pub const EPSILON: f32 = 0.00001; diff --git a/rtchallenge/src/tuples.rs b/rtchallenge/src/tuples.rs index 46379de..4f798be 100644 --- a/rtchallenge/src/tuples.rs +++ b/rtchallenge/src/tuples.rs @@ -1,5 +1,7 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; +use crate::EPSILON; + #[derive(Debug, Copy, Clone)] pub struct Tuple { pub x: f32, @@ -116,10 +118,10 @@ impl Sub for Tuple { impl PartialEq for Tuple { fn eq(&self, rhs: &Tuple) -> bool { - ((self.x - rhs.x).abs() < f32::EPSILON) - && ((self.y - rhs.y).abs() < f32::EPSILON) - && ((self.z - rhs.z).abs() < f32::EPSILON) - && ((self.w - rhs.w).abs() < f32::EPSILON) + ((self.x - rhs.x).abs() < EPSILON) + && ((self.y - rhs.y).abs() < EPSILON) + && ((self.z - rhs.z).abs() < EPSILON) + && ((self.w - rhs.w).abs() < EPSILON) } } pub fn dot(a: Tuple, b: Tuple) -> f32 { @@ -221,7 +223,7 @@ impl Sub for Color { #[cfg(test)] mod tests { - use super::{cross, dot, Color, Tuple}; + use super::{cross, dot, Color, Tuple, EPSILON}; #[test] fn is_point() { // A tuple with w = 1 is a point @@ -326,7 +328,7 @@ mod tests { #[test] fn vector_normalize_magnitude() { let len = Tuple::vector(1., 2., 3.).normalize().magnitude(); - assert!((1. - len).abs() < f32::EPSILON); + assert!((1. - len).abs() < EPSILON); } #[test] fn dot_two_tuples() {