aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Sphere.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/Math/Sphere.hs')
-rw-r--r--Spear/Math/Sphere.hs21
1 files changed, 18 insertions, 3 deletions
diff --git a/Spear/Math/Sphere.hs b/Spear/Math/Sphere.hs
index 197a9b2..1d20275 100644
--- a/Spear/Math/Sphere.hs
+++ b/Spear/Math/Sphere.hs
@@ -1,9 +1,17 @@
1{-# LANGUAGE MultiParamTypeClasses #-}
2{-# LANGUAGE NoImplicitPrelude #-}
3
1module Spear.Math.Sphere 4module Spear.Math.Sphere
2where 5where
3 6
4import Spear.Math.Vector 7import Spear.Math.Algebra
8import Spear.Math.Spatial
9import Spear.Math.Spatial3
10import Spear.Math.Vector
11import Spear.Prelude
12
13import Data.List (foldl')
5 14
6import Data.List (foldl')
7 15
8-- | A sphere in 3D space. 16-- | A sphere in 3D space.
9data Sphere = Sphere 17data Sphere = Sphere
@@ -11,12 +19,19 @@ data Sphere = Sphere
11 , radius :: {-# UNPACK #-} !Float 19 , radius :: {-# UNPACK #-} !Float
12 } 20 }
13 21
22
23instance Positional Sphere Vector3 where
24 setPosition p sphere = sphere { center = p }
25 position = center
26 translate v sphere = sphere { center = center sphere + v }
27
28
14-- | Create a sphere from the given points. 29-- | Create a sphere from the given points.
15sphere :: [Vector3] -> Sphere 30sphere :: [Vector3] -> Sphere
16sphere [] = Sphere zero3 0 31sphere [] = Sphere zero3 0
17sphere (x:xs) = Sphere c r 32sphere (x:xs) = Sphere c r
18 where 33 where
19 c = pmin + (pmax-pmin)/2 34 c = pmin + (pmax-pmin) / (2::Float)
20 r = norm $ pmax - c 35 r = norm $ pmax - c
21 (pmin,pmax) = foldl' update (x,x) xs 36 (pmin,pmax) = foldl' update (x,x) xs
22 update (pmin,pmax) p = (min p pmin, max p pmax) 37 update (pmin,pmax) p = (min p pmin, max p pmax)