Last active
July 29, 2020 15:19
-
-
Save richardpringle/add33652fbecc05a9682bf277565b753 to your computer and use it in GitHub Desktop.
Ray Tracer Vectors and Points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Vector<T> { | |
tuple: Tuple<T>, | |
} | |
struct Point<T> { | |
tuple: Tuple<T>, | |
} | |
impl<T: From<u8>> Vector<T> { | |
fn new(x: T, y: T, z: T) -> Self { | |
Self { | |
tuple: Tuple(x, y, z, T::from(0)), | |
} | |
} | |
} | |
impl<T: From<u8>> Point<T> { | |
fn new(x: T, y: T, z: T) -> Self { | |
Self { | |
tuple: Tuple(x, y, z, T::from(1)), | |
} | |
} | |
} | |
impl<T: Neg<Output = T>> Neg for Point<T> { | |
type Output = Self; | |
fn neg(self) -> Self::Output { | |
Self { tuple: -self.tuple } | |
} | |
} | |
impl<T: Neg<Output = T>> Neg for Vector<T> { | |
type Output = Self; | |
fn neg(self) -> Self::Output { | |
Self { tuple: -self.tuple } | |
} | |
} | |
impl<T: Add<Output = T>> Add for Vector<T> { | |
type Output = Self; | |
fn add(self, rhs: Self) -> Self::Output { | |
Self { | |
tuple: self.tuple + rhs.tuple, | |
} | |
} | |
} | |
impl<T: Add<Output = T>> Add<Point<T>> for Vector<T> { | |
type Output = Point<T>; | |
fn add(self, rhs: Point<T>) -> Self::Output { | |
Point { | |
tuple: self.tuple + rhs.tuple, | |
} | |
} | |
} | |
impl<T: Add<Output = T>> Add<Vector<T>> for Point<T> { | |
type Output = Self; | |
fn add(self, rhs: Vector<T>) -> Self::Output { | |
Self { | |
tuple: self.tuple + rhs.tuple, | |
} | |
} | |
} | |
impl<T: Sub<Output = T>> Sub for Vector<T> { | |
type Output = Self; | |
fn sub(self, rhs: Self) -> Self::Output { | |
Self { | |
tuple: self.tuple - rhs.tuple, | |
} | |
} | |
} | |
impl<T: Sub<Output = T>> Sub<Vector<T>> for Point<T> { | |
type Output = Self; | |
fn sub(self, rhs: Vector<T>) -> Self::Output { | |
Self { | |
tuple: self.tuple - rhs.tuple, | |
} | |
} | |
} | |
impl<T: Sub<Output = T>> Sub for Point<T> { | |
type Output = Vector<T>; | |
fn sub(self, rhs: Self) -> Self::Output { | |
Vector { | |
tuple: self.tuple - rhs.tuple, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment