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.hs26
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 @@
1module Spear.Math.Sphere
2where
3
4import Spear.Math.Vector
5
6import Data.List (foldl')
7
8-- | A sphere in 3D space.
9data Sphere = Sphere
10 { center :: {-# UNPACK #-} !Vector3
11 , radius :: {-# UNPACK #-} !Float
12 }
13
14-- | Create a sphere from the given points.
15sphere :: [Vector3] -> Sphere
16sphere [] = Sphere zero3 0
17sphere (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.
25circlept :: Sphere -> Vector3 -> Bool
26circlept (Sphere c r) p = r*r >= normSq (p - c)