Implementing Div Trait
This commit is contained in:
parent
07b2fc28d8
commit
31e224f9b0
34
src/main.rs
34
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<f64> for Vector3 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vector3> 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<f64> 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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user