Some changes

This commit is contained in:
Stephan Rehfeld 2026-06-17 17:34:29 +02:00
parent 5a23f49eae
commit 3eca1b1f7b

View File

@ -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<T> {
x: T,
y: T,
z: T,
}
impl Vector3 {
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
impl<T> Vector3<T> {
pub fn new(x: T, y: T, z: T) -> Vector3<T> {
Vector3 { x, y, z }
}
}
impl Add for Vector3 {
type Output = Vector3;
impl<T> Add for Vector3<T>
where
T: Add<Output = T>,
{
type Output = Vector3<T>;
fn add(self, rhs: Vector3) -> Self::Output {
fn add(self, rhs: Vector3<T>) -> Self::Output {
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Vector3 {
type Output = Vector3;
impl<T> Sub for Vector3<T>
where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
fn sub(self, rhs: Vector3) -> Self::Output {
fn sub(self, rhs: Vector3<T>) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl Mul<f64> for Vector3 {
type Output = Vector3;
impl<T> Mul<T> for Vector3<T>
where
T: Mul<Output = T> + Clone + Copy,
{
type Output = Vector3<T>;
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<Vector3> for f64 {
type Output = Vector3;
/*
impl<T> Mul<Vector3<T>> for f64 {
type Output = Vector3<T>;
fn mul(self, rhs: Vector3) -> Self::Output {
fn mul(self, rhs: Vector3<T>) -> Self::Output {
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
}
}
}*/
impl Mul for Vector3 {
type Output = f64;
impl<T> Mul for Vector3<T>
where
T: Mul<Output = T> + Add<Output = T>,
{
type Output = T;
fn mul(self, rhs: Vector3) -> Self::Output {
fn mul(self, rhs: Vector3<T>) -> Self::Output {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
}
impl Div<f64> for Vector3 {
type Output = Vector3;
impl<T> Div<T> for Vector3<T>
where
T: Div<Output = T> + Copy + Clone,
{
type Output = Vector3<T>;
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<Vector3> for Point3 {
impl Add<Vector3<T>> for Point3 {
type Output = Point3;
fn add(self, rhs: Vector3) -> Self::Output {
fn add(self, rhs: Vector3<T>) -> 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<T>;
fn sub(self, rhs: Point3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)