From 3eca1b1f7b4f44fbe9775509af17169024313a3c Mon Sep 17 00:00:00 2001 From: Stephan Rehfeld Date: Wed, 17 Jun 2026 17:34:29 +0200 Subject: [PATCH] Some changes --- src/math.rs | 72 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/src/math.rs b/src/math.rs index fa5b481..988841e 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,62 +1,78 @@ use std::ops::{Add, Div, Mul, Sub}; #[derive(Copy, Clone, PartialEq, Debug)] -pub struct Vector3 { - x: f64, - y: f64, - z: f64, +pub struct Vector3 { + x: T, + y: T, + z: T, } -impl Vector3 { - pub fn new(x: f64, y: f64, z: f64) -> Vector3 { +impl Vector3 { + pub fn new(x: T, y: T, z: T) -> Vector3 { Vector3 { x, y, z } } } -impl Add for Vector3 { - type Output = Vector3; +impl Add for Vector3 +where + T: Add, +{ + type Output = Vector3; - fn add(self, rhs: Vector3) -> Self::Output { + fn add(self, rhs: Vector3) -> Self::Output { Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) } } -impl Sub for Vector3 { - type Output = Vector3; +impl Sub for Vector3 +where + T: Sub, +{ + type Output = Vector3; - fn sub(self, rhs: Vector3) -> Self::Output { + fn sub(self, rhs: Vector3) -> Self::Output { Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) } } -impl Mul for Vector3 { - type Output = Vector3; +impl Mul for Vector3 +where + T: Mul + Clone + Copy, +{ + type Output = Vector3; - fn mul(self, rhs: f64) -> Self::Output { + fn mul(self, rhs: T) -> Self::Output { Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs) } } -impl Mul for f64 { - type Output = Vector3; +/* +impl Mul> for f64 { + type Output = Vector3; - fn mul(self, rhs: Vector3) -> Self::Output { + fn mul(self, rhs: Vector3) -> Self::Output { Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z) } -} +}*/ -impl Mul for Vector3 { - type Output = f64; +impl Mul for Vector3 +where + T: Mul + Add, +{ + type Output = T; - fn mul(self, rhs: Vector3) -> Self::Output { + fn mul(self, rhs: Vector3) -> Self::Output { self.x * rhs.x + self.y * rhs.y + self.z * rhs.z } } -impl Div for Vector3 { - type Output = Vector3; +impl Div for Vector3 +where + T: Div + Copy + Clone, +{ + type Output = Vector3; - fn div(self, rhs: f64) -> Self::Output { + fn div(self, rhs: T) -> Self::Output { Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs) } } @@ -74,16 +90,16 @@ impl Point3 { } } -impl Add for Point3 { +impl Add> for Point3 { type Output = Point3; - fn add(self, rhs: Vector3) -> Self::Output { + fn add(self, rhs: Vector3) -> Self::Output { Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) } } impl Sub for Point3 { - type Output = Vector3; + type Output = Vector3; fn sub(self, rhs: Point3) -> Self::Output { Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)