diff options
Diffstat (limited to 'Spear/Math/Sphere.hs')
-rw-r--r-- | Spear/Math/Sphere.hs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Spear/Math/Sphere.hs b/Spear/Math/Sphere.hs new file mode 100644 index 0000000..9c80811 --- /dev/null +++ b/Spear/Math/Sphere.hs | |||
@@ -0,0 +1,26 @@ | |||
1 | module Spear.Math.Sphere | ||
2 | where | ||
3 | |||
4 | import Spear.Math.Vector | ||
5 | |||
6 | import Data.List (foldl') | ||
7 | |||
8 | -- | A sphere in 3D space. | ||
9 | data Sphere = Sphere | ||
10 | { center :: {-# UNPACK #-} !Vector3 | ||
11 | , radius :: {-# UNPACK #-} !Float | ||
12 | } | ||
13 | |||
14 | -- | Create a sphere from the given points. | ||
15 | sphere :: [Vector3] -> Sphere | ||
16 | sphere [] = Sphere zero3 0 | ||
17 | sphere (x:xs) = Sphere c r | ||
18 | where | ||
19 | c = pmin + (pmax-pmin)/2 | ||
20 | r = norm $ pmax - c | ||
21 | (pmin,pmax) = foldl' update (x,x) xs | ||
22 | update (pmin,pmax) p = (min p pmin, max p pmax) | ||
23 | |||
24 | -- | Return 'True' if the given sphere contains the given point, 'False' otherwise. | ||
25 | circlept :: Sphere -> Vector3 -> Bool | ||
26 | circlept (Sphere c r) p = r*r >= normSq (p - c) | ||