Impl Trait add for Point ad Vector3

This commit is contained in:
Stephan Rehfeld 2026-04-29 18:08:30 +02:00
parent 31e224f9b0
commit 9ef8e08936

View File

@ -61,6 +61,35 @@ impl Div<f64> 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<Vector3> 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);
}