diff --git a/src/main.rs b/src/main.rs index 17846c0..da90604 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, Mul, Sub}; +use std::ops::{Add, Div, Mul, Sub}; #[derive(Copy, Clone)] struct Vector3 { @@ -37,15 +37,45 @@ impl Mul for Vector3 { } } +impl Mul for f64 { + type Output = Vector3; + + 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; + + 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; + + fn div(self, rhs: f64) -> Self::Output { + Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs) + } +} + fn main() { let vec1 = Vector3::new(1.0, 0.0, 0.0); - let vec2 = Vector3::new(0.0, 1.0, 0.0); + let vec2 = Vector3::new(1.0, 1.0, 0.0); let vec3 = vec1 + vec2; let vec4 = vec1 - vec2; let vec5 = vec4 * 3.14159; + let vec6 = 3.14159 * vec3; + let dot_product = vec1 * vec2; + let vec7 = vec6 / 3.14159; println!("x = {}, y = {}, z = {}", vec3.x, vec3.y, vec3.z); println!("x = {}, y = {}, z = {}", vec4.x, vec4.y, vec4.z); println!("x = {}, y = {}, z = {}", vec5.x, vec5.y, vec5.z); + 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); }