Skip to content

Instantly share code, notes, and snippets.

View richardpringle's full-sized avatar
🐢
building things

Richard Pringle richardpringle

🐢
building things
View GitHub Profile
@richardpringle
richardpringle / tuple-types.rs
Created July 29, 2020 16:19
Ray Tracer Cross and Dot Product
impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Neg<Output = T> + From<u8> + Copy> Vector<T> {
fn dot(&self, rhs: Self) -> T {
let (Tuple(x1, y1, z1, _), Tuple(x2, y2, z2, _)) = (self.tuple, rhs.tuple);
(x1 * x2) + (y1 * y1) + (z1 * z2)
}
fn cross(&self, rhs: Self) -> Self {
let (Tuple(x1, y1, z1, _), Tuple(x2, y2, z2, _)) = (self.tuple, rhs.tuple);
Self::new(
(y1 * z2) - (z1 * y2),
@richardpringle
richardpringle / tuple-types.rs
Created July 29, 2020 16:08
Ray Tracer Vector Magnitude and Norm
impl Vector<f64> {
fn magnitude(&self) -> f64 {
let Tuple(x, y, z, _) = self.tuple;
(x.powi(2) + y.powi(2) + z.powi(2)).sqrt()
}
fn norm(&self) -> Self {
*self * (1.0 / self.magnitude())
}
}
@richardpringle
richardpringle / tuple-types.rs
Created July 29, 2020 15:56
Ray Tracer Scalar Multiplication and Division of a Vector
impl<T: Mul<Output = T> + Copy> Mul<T> for Vector<T> {
type Output = Self;
fn mul(self, rhs: T) -> Self::Output {
Self {
tuple: self.tuple * rhs,
}
}
}
@richardpringle
richardpringle / tuple.rs
Created July 29, 2020 15:49
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> {
@richardpringle
richardpringle / tuple-types.rs
Last active July 29, 2020 15:19
Ray Tracer Vectors and Points
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 {
@richardpringle
richardpringle / tuple.rs
Created July 29, 2020 13:48
Ray Tracer Generic Tuple
use std::ops::{Add, Div, Mul, Neg, Sub};
struct Tuple<T>(T, T, T, T);
impl<T: Neg<Output = T>> Neg for Tuple<T> {
type Output = Self;
fn neg(self) -> Self::Output {
let Self(x, y, z, w) = self;
Self(-x, -y, -z, -w)
@richardpringle
richardpringle / tuple.rs
Last active July 29, 2020 13:38
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)
@richardpringle
richardpringle / git-debugging.bash
Created April 21, 2020 20:32
File Isolation with git while debugging
# I've just refactored a bunch of my code, changing 2 files
$ run tests
# one test is broken, time to isolate
$ git add . && git commit -m WIP # commit my files
$ WIP=$(git rev-parse --short HEAD) # save the commit hash
$ git reset --hard HEAD~1 # reset my branch to the last known working state
$ git checkout $WIP -- file_1 # see if this file cause the issue
$ run tests
# tests pass, must be a different file