diff options
Diffstat (limited to 'Spear/Math/Sphere.hs')
-rw-r--r-- | Spear/Math/Sphere.hs | 21 |
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 | |||
1 | module Spear.Math.Sphere | 4 | module Spear.Math.Sphere |
2 | where | 5 | where |
3 | 6 | ||
4 | import Spear.Math.Vector | 7 | import Spear.Math.Algebra |
8 | import Spear.Math.Spatial | ||
9 | import Spear.Math.Spatial3 | ||
10 | import Spear.Math.Vector | ||
11 | import Spear.Prelude | ||
12 | |||
13 | import Data.List (foldl') | ||
5 | 14 | ||
6 | import Data.List (foldl') | ||
7 | 15 | ||
8 | -- | A sphere in 3D space. | 16 | -- | A sphere in 3D space. |
9 | data Sphere = Sphere | 17 | data Sphere = Sphere |
@@ -11,12 +19,19 @@ data Sphere = Sphere | |||
11 | , radius :: {-# UNPACK #-} !Float | 19 | , radius :: {-# UNPACK #-} !Float |
12 | } | 20 | } |
13 | 21 | ||
22 | |||
23 | instance 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. |
15 | sphere :: [Vector3] -> Sphere | 30 | sphere :: [Vector3] -> Sphere |
16 | sphere [] = Sphere zero3 0 | 31 | sphere [] = Sphere zero3 0 |
17 | sphere (x:xs) = Sphere c r | 32 | sphere (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) |