Skip to content

Instantly share code, notes, and snippets.

@richardpringle
Last active July 29, 2020 13:38
Show Gist options
  • Save richardpringle/c93be4a95190c4ae603c161e4e319bde to your computer and use it in GitHub Desktop.
Save richardpringle/c93be4a95190c4ae603c161e4e319bde to your computer and use it in GitHub Desktop.
Ray Tracer Tuple
use std::ops::{Add, Div, Mul, Neg, Sub};
struct Tuple(i32, i32, i32, i32);
impl Neg for Tuple {
type Output = Self;
fn neg(self) -> Self::Output {
let Self(x, y, z, w) = self;
Self(-x, -y, -z, -w)
}
}
impl Add for Tuple {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let (Self(x1, y1, z1, w1), Self(x2, y2, z2, w2)) = (self, rhs);
Self(x1 + x2, y1 + y2, z1 + z2, w1 + w2)
}
}
impl Sub for Tuple {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
let (Self(x1, y1, z1, w1), Self(x2, y2, z2, w2)) = (self, rhs);
Self(x1 - x2, y1 - y2, z1 - z2, w1 - w2)
}
}
impl Mul for Tuple {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
let (Self(x1, y1, z1, w1), Self(x2, y2, z2, w2)) = (self, rhs);
Self(x1 * x2, y1 * y2, z1 * z2, w1 * w2)
}
}
impl Div for Tuple {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
let (Self(x1, y1, z1, w1), Self(x2, y2, z2, w2)) = (self, rhs);
Self(x1 / x2, y1 / y2, z1 / z2, w1 / w2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment