From a78ba3d9b3c64a2dd344e69f39d93a367e58e144 Mon Sep 17 00:00:00 2001 From: ratnajagadeesharava Date: Thu, 11 Dec 2025 10:58:29 +0530 Subject: [PATCH] implimented substraction and multiplication, added test cases for it --- src/util/tuple/tuple3.rs | 46 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/util/tuple/tuple3.rs b/src/util/tuple/tuple3.rs index 8ca2899..1bc160c 100644 --- a/src/util/tuple/tuple3.rs +++ b/src/util/tuple/tuple3.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, Index, Mul}; +use std::ops::{Add, Index, Mul, Sub}; #[derive(Debug)] pub struct Tuple3 { pub x: T, @@ -39,6 +39,33 @@ impl Index for Tuple3 { } } } +impl Sub for Tuple3 +where + T: Sub, +{ + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { + Self { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + } + } +} + +impl Mul for Tuple3 +where + T: Mul, +{ + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + Self { + x: self.x * rhs.x, + y: self.y * rhs.y, + z: self.z * rhs.z, + } + } +} impl Add for Tuple3 where T: Add, @@ -61,7 +88,22 @@ fn add_tupple_3() { let result = Tuple3::new(3, 5, 8); assert_eq!(result, tup1); } - +#[test] +fn mul_tupple_3() { + let mut tup1 = Tuple3::new(1, 2, 4); + let mut tup2 = Tuple3::new(2, 3, 4); + tup1 = tup2 * tup1; + let result = Tuple3::new(2, 6, 16); + assert_eq!(result, tup1); +} +#[test] +fn sub_tupple_3() { + let mut tup1 = Tuple3::new(1, 2, 4); + let mut tup2 = Tuple3::new(2, 3, 4); + tup1 = tup1 - tup2; + let result = Tuple3::new(-1, -1, 0); + assert_eq!(result, tup1); +} #[test] fn check_dot() { let tup1 = Tuple3::new(1, 2, 4);