aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2019-01-30 22:27:59 +0000
committerYann Herklotz <ymherklotz@gmail.com>2019-01-30 22:27:59 +0000
commit85a6ddf10914c5798db5044b604aae31551fe780 (patch)
tree40efcd50ab53fcbf4cb51e637d2cab3c7b0f28e6
parent46748a4bedeea5ff35a9d0c627f9ca1f8dee8bcc (diff)
downloadmirror-ball-85a6ddf10914c5798db5044b604aae31551fe780.tar.gz
mirror-ball-85a6ddf10914c5798db5044b604aae31551fe780.zip
Added reflections
-rw-r--r--src/Main.hs3
-rw-r--r--src/Vec.hs11
2 files changed, 13 insertions, 1 deletions
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