From 85a6ddf10914c5798db5044b604aae31551fe780 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 30 Jan 2019 22:27:59 +0000 Subject: Added reflections --- src/Main.hs | 3 +++ src/Vec.hs | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Main.hs b/src/Main.hs index b92807c..b674eb6 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -52,3 +52,6 @@ main = do let i = circleImage (normalise 511) 511 BL.writeFile "normal.ppm" . encodePPM . clampImage $ i BL.writeFile "normal.pfm" . encode . revColour $ i + let r = circleImage (reflect 511 (Vec (0, 0, 1))) 511 + BL.writeFile "reflect.ppm" . encodePPM . clampImage $ r + BL.writeFile "reflect.pfm" . encode . revColour $ r diff --git a/src/Vec.hs b/src/Vec.hs index 19a8485..9e4fdbd 100644 --- a/src/Vec.hs +++ b/src/Vec.hs @@ -18,6 +18,14 @@ newtype Vec a = Vec { unVec :: (a, a, a) } instance Functor Vec where fmap f (Vec (a, b, c)) = Vec (f a, f b, f c) +instance (Num a) => Num (Vec a) where + (Vec (x1, y1, z1)) + (Vec (x2, y2, z2)) = Vec (x1 + x2, y1 + y2, z1 + z2) + (Vec (x1, y1, z1)) - (Vec (x2, y2, z2)) = Vec (x1 - x2, y1 - y2, z1 - z2) + (Vec (x1, y1, z1)) * (Vec (x2, y2, z2)) = Vec (x1 * x2, y1 * y2, z1 * z2) + abs = fmap abs + signum = fmap signum + fromInteger i = Vec (fromInteger i, 0, 0) + findZ :: (Floating a) => a -> a -> Vec a findZ x y = Vec (x, y, sqrt (1 - x^^2 - y^^2)) @@ -34,6 +42,7 @@ normalise size (y, x) = reflect :: (Floating a) => Int -> Vec a -> (Int, Int) -> Vec a reflect size v (y, x) = - n + l - v where n = normalise size (y, x) + l = ((2 * dot n v)*) <$> n -- cgit