Skip to content

Instantly share code, notes, and snippets.

@richardpringle
Created July 29, 2020 15:49
Show Gist options
  • Save richardpringle/7d5e9004fdf7cde30392331ae63bb0f2 to your computer and use it in GitHub Desktop.
Save richardpringle/7d5e9004fdf7cde30392331ae63bb0f2 to your computer and use it in GitHub Desktop.
Ray Tracer Scalar Division and Multiplication
impl<T: Mul<Output = T> + Copy> Mul<T> for Tuple<T> {
type Output = Self;
fn mul(self, rhs: T) -> Self::Output {
let Self(x, y, z, w) = self;
Self(x * rhs, y * rhs, z * rhs, w * rhs)
}
}
impl<T: Div<Output = T> + Copy> Div<T> for Tuple<T> {
type Output = Self;
fn div(self, rhs: T) -> Self::Output {
let Self(x, y, z, w) = self;
Self(x / rhs, y / rhs, z / rhs, w / rhs)
}
}
@richardpringle
Copy link
Author

How do I reverse the order for multiplication?
impl<T: Mul<Output = T> + Copy> Mul<Tuple<T>> for T { // ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment