aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2019-01-30 22:11:05 +0000
committerYann Herklotz <ymherklotz@gmail.com>2019-01-30 22:11:05 +0000
commit4280f50fd134c1ac505e746c9418f94d230d9a00 (patch)
tree00dd31aff9389d5fcde99a084e1b0a7685383474
parentac8f5dad6b8ee7f178488a3ade34d789f50c8d1f (diff)
downloadmirror-ball-4280f50fd134c1ac505e746c9418f94d230d9a00.tar.gz
mirror-ball-4280f50fd134c1ac505e746c9418f94d230d9a00.zip
Add Vec module
-rw-r--r--src/Vec.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Vec.hs b/src/Vec.hs
new file mode 100644
index 0000000..19a8485
--- /dev/null
+++ b/src/Vec.hs
@@ -0,0 +1,39 @@
+{-|
+Module : Vec
+Description : Small Vector module
+Copyright : (c) 2019, Yann Herklotz Grave
+License : GPL-3
+Maintainer : ymherklotz [at] gmail [dot] com
+Stability : experimental
+Portability : POSIX
+
+Small Vector module
+-}
+
+module Vec where
+
+newtype Vec a = Vec { unVec :: (a, a, a) }
+ deriving (Show)
+
+instance Functor Vec where
+ fmap f (Vec (a, b, c)) = Vec (f a, f b, f c)
+
+findZ :: (Floating a) => a -> a -> Vec a
+findZ x y =
+ Vec (x, y, sqrt (1 - x^^2 - y^^2))
+
+dot :: (Num a) => Vec a -> Vec a -> a
+dot (Vec (x1, y1, z1)) (Vec (x2, y2, z2)) =
+ x1 * x2 + y1 * y2 + z1 * z2
+
+normalise :: (Floating a) => Int -> (Int, Int) -> Vec a
+normalise size (y, x) =
+ findZ (scale x) $ scale y
+ where
+ scale a = 2 * fromIntegral a / fromIntegral size - 1
+
+reflect :: (Floating a) => Int -> Vec a -> (Int, Int) -> Vec a
+reflect size v (y, x) =
+ n
+ where
+ n = normalise size (y, x)