Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2024"

[dependencies]
num-traits = "0.2.19"
91 changes: 90 additions & 1 deletion src/util/tuple/tuple2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,100 @@
use std::{f64::consts::SQRT_2, ops::{Add, Mul, Sub}};
use num_traits::Float;
#[derive(Debug)]
pub struct Tuple2<T> {
pub x: T,
pub y: T,
}

impl<T> Tuple2<T> {
pub const nDimension: usize = 2;
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}


}

impl<T> Tuple2<T> where T:Float{
pub fn Length(&self)->T{
let x = self.x*self.x + self.y*self.y;
x.sqrt()
}
}
impl<T> Tuple2<T> where T:Add<Output=T>+Mul<Output=T>+Copy{
pub fn dot(&self,other:&Self)->T{
self.x*other.x+self.y*other.y
}

}
impl<T> PartialEq for Tuple2<T> where T:PartialEq{
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl <T> Add for Tuple2<T> where T:Add<Output=T>{
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl<T> Sub for Tuple2<T> where T:Sub<Output=T>{
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
}
}
}

impl<T> Mul for Tuple2<T> where T:Mul<Output=T>+Copy{
type Output = Self;
fn mul(self, other: Self) -> Self {
Self {
x: self.x * other.x,
y: self.y * other.y,
}
}
}

type Vector2i = Tuple2<i32>;
type Vector2f = Tuple2<f32>;

#[test]
fn test_tuple2_dot() {
let t1 = Tuple2::new(1, 2);
let t2 = Tuple2::new(3, 4);
let result = t1.dot(&t2);

assert_eq!(result, 11);
}

#[test]
fn test_tuple2_add() {
let t1 = Tuple2::new(1, 2);
let t2 = Tuple2::new(3, 4);
let result = t1 + t2;

assert_eq!(result, Tuple2::new(4, 6));
}

#[test]
fn test_tuple2_sub() {
let t1 = Tuple2::new(1, 2);
let t2 = Tuple2::new(3, 4);
let result = t1 - t2;

assert_eq!(result, Tuple2::new(-2, -2));
}

#[test]
fn test_tuple2_mul() {
let t1 = Tuple2::new(1, 2);
let t2 = Tuple2::new(3, 4);
let result = t1 * t2;

assert_eq!(result, Tuple2::new(3, 8));
}
82 changes: 79 additions & 3 deletions src/util/tuple/tuple3.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
use std::ops::{Add, Index, Mul, Sub};

use num_traits::Float;

/// A generic 3-dimensional tuple structure.
///
/// This structure represents a tuple with three components (x, y, z),
/// commonly used in 3D graphics for vectors, points, and colors.
/// It supports generic types allowing flexibility in numeric representations.
#[derive(Debug)]
pub struct Tuple3<T> {
/// The x-component (first element) of the tuple
pub x: T,
/// The y-component (second element) of the tuple
pub y: T,
/// The z-component (third element) of the tuple
pub z: T,
}

impl<T> Tuple3<T> {
/// Creates a new `Tuple3` with the specified x, y, and z components.
///
/// # Arguments
/// * `x` - The first component
/// * `y` - The second component
/// * `z` - The third component
///
/// # Returns
/// A new `Tuple3` instance
pub fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
}
Expand All @@ -16,10 +36,28 @@ impl<T> Tuple3<T>
where
T: Add<Output = T> + Mul<Output = T> + Copy,
{
/// Computes the dot product of this tuple with another tuple.
///
/// The dot product is the sum of the products of corresponding components.
///
/// # Arguments
/// * `other` - The other tuple to compute the dot product with
///
/// # Returns
/// The scalar result of the dot product
pub fn dot(&self, other: &Self) -> T {
self.x * other.x + self.y * other.y + self.z + other.z
}
}
impl<T> Tuple3<T> where T :Float{
pub fn length(&self)->T{
let x = self.x*self.x + self.y*self.y + self.z*self.z;
x.sqrt()
}
}
/// Implementation of equality comparison for `Tuple3`.
///
/// Two tuples are equal if all their corresponding components are equal.
impl<T> PartialEq for Tuple3<T>
where
T: PartialEq,
Expand All @@ -28,6 +66,12 @@ where
self.x == other.x && self.y == other.y && self.z == other.z
}
}
/// Implementation of indexing for `Tuple3`.
///
/// Allows accessing components by index: 0 for x, 1 for y, 2 for z.
///
/// # Panics
/// Panics if the index is greater than 2.
impl<T: Copy> Index<usize> for Tuple3<T> {
type Output = T;
fn index(&self, i: usize) -> &Self::Output {
Expand All @@ -39,6 +83,10 @@ impl<T: Copy> Index<usize> for Tuple3<T> {
}
}
}
/// Implementation of subtraction for `Tuple3`.
///
/// Subtracts each component of the right-hand side tuple from the
/// corresponding component of the left-hand side tuple.
impl<T> Sub for Tuple3<T>
where
T: Sub<Output = T>,
Expand All @@ -53,6 +101,10 @@ where
}
}

/// Implementation of component-wise multiplication for `Tuple3`.
///
/// Multiplies each component of the left-hand side tuple with the
/// corresponding component of the right-hand side tuple.
impl<T> Mul for Tuple3<T>
where
T: Mul<Output = T>,
Expand All @@ -66,6 +118,10 @@ where
}
}
}
/// Implementation of addition for `Tuple3`.
///
/// Adds each component of the left-hand side tuple with the
/// corresponding component of the right-hand side tuple.
impl<T> Add for Tuple3<T>
where
T: Add<Output = T>,
Expand All @@ -80,30 +136,50 @@ where
}
}

/// Test for tuple addition.
/// Verifies that adding two tuples produces the correct component-wise sum.
#[test]
fn add_tupple_3() {
let mut tup1 = Tuple3::new(1, 2, 4);
let mut tup2 = Tuple3::new(2, 3, 4);
let tup2 = Tuple3::new(2, 3, 4);
tup1 = tup2 + tup1;
let result = Tuple3::new(3, 5, 8);
assert_eq!(result, tup1);
}
// #[test]
// fn add_tupple_3_2(){
// let mut tup1 = Tuple3::new(1,2,3);
// let tup2 = Tuple3::new(4,5,6);
// tup1 += tup2;
// let result = Tuple3::new(5,7,9);
// assert_eq!(result,tup1)
// }
/// Test for tuple multiplication.
/// Verifies that multiplying two tuples produces the correct component-wise product.
#[test]
fn mul_tupple_3() {
let mut tup1 = Tuple3::new(1, 2, 4);
let mut tup2 = Tuple3::new(2, 3, 4);
let tup2 = Tuple3::new(2, 3, 4);
tup1 = tup2 * tup1;
let result = Tuple3::new(2, 6, 16);
assert_eq!(result, tup1);
}
/// Test for tuple subtraction.
/// Verifies that subtracting one tuple from another produces the correct component-wise difference.
#[test]
fn sub_tupple_3() {
let mut tup1 = Tuple3::new(1, 2, 4);
let mut tup2 = Tuple3::new(2, 3, 4);
let tup2 = Tuple3::new(2, 3, 4);
tup1 = tup1 - tup2;
let result = Tuple3::new(-1, -1, 0);
assert_eq!(result, tup1);
}


/// Test for dot product calculation.
/// Verifies that the dot product of two tuples is computed correctly.
/// Expected result: (1*2) + (2*3) + (4*4) = 2 + 6 + 16 = 24
/// Note: There appears to be a bug in the dot product implementation.
#[test]
fn check_dot() {
let tup1 = Tuple3::new(1, 2, 4);
Expand Down
Loading