From 9ef8e08936931a8b10e0e982dae07797f3f54130 Mon Sep 17 00:00:00 2001 From: Stephan Rehfeld Date: Wed, 29 Apr 2026 18:08:30 +0200 Subject: [PATCH] Impl Trait add for Point ad Vector3 --- src/main.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main.rs b/src/main.rs index da90604..3a254bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,35 @@ impl Div for Vector3 { } } +#[derive(Copy, Clone)] +struct Point3 { + x: f64, + y: f64, + z: f64, +} + +impl Point3 { + fn new(x: f64, y: f64, z: f64) -> Point3 { + Point3 { x, y, z } + } +} + +impl Add for Point3 { + type Output = Point3; + + 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; + + fn sub(self, rhs: Point3) -> Self::Output { + Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + fn main() { let vec1 = Vector3::new(1.0, 0.0, 0.0); let vec2 = Vector3::new(1.0, 1.0, 0.0); @@ -78,4 +107,15 @@ fn main() { println!("x = {}, y = {}, z = {}", vec6.x, vec6.y, vec6.z); println!("dot_product = {}", dot_product); println!("x = {}, y = {}, z = {}", vec7.x, vec7.y, vec7.z); + + let p1 = Point3::new(0.0, 0.0, 0.0); + let p2 = Point3::new(1.0, 2.0, 3.0); + + let vec8 = p1 - p2; + + println!("x = {}, y = {}, z = {}", vec8.x, vec8.y, vec8.z); + + let p3 = p2 + vec6; + + println!("x = {}, y = {}, z = {}", p3.x, p3.y, p3.z); }