From 385ed70d88906d8c5841f906b19a4a06d235d829 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 17 Jul 2021 09:30:28 -0700 Subject: [PATCH] tuples: implement EPSILON aware PartialEq for Color. Mark Color::new const so it can be used in const expressions (like predefining colors). --- rtchallenge/src/tuples.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rtchallenge/src/tuples.rs b/rtchallenge/src/tuples.rs index 681819f..7e6dcf0 100644 --- a/rtchallenge/src/tuples.rs +++ b/rtchallenge/src/tuples.rs @@ -158,17 +158,24 @@ pub fn cross(a: Tuple, b: Tuple) -> Tuple { ) } -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug)] pub struct Color { pub red: f32, pub green: f32, pub blue: f32, } impl Color { - pub fn new(red: f32, green: f32, blue: f32) -> Color { + pub const fn new(red: f32, green: f32, blue: f32) -> Color { Color { red, green, blue } } } +impl PartialEq for Color { + fn eq(&self, rhs: &Color) -> bool { + ((self.red - rhs.red).abs() < EPSILON) + && ((self.green - rhs.green).abs() < EPSILON) + && ((self.blue - rhs.blue).abs() < EPSILON) + } +} impl Add for Color { type Output = Self; fn add(self, other: Self) -> Self {