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.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/Spear/Math/Sphere.hs b/Spear/Math/Sphere.hs
new file mode 100644
index 0000000..4a9e3fc
--- /dev/null
+++ b/Spear/Math/Sphere.hs
@@ -0,0 +1,35 @@
1module Spear.Math.Sphere
2(
3 Sphere(..)
4, sphere
5, spherept
6)
7where
8
9
10import Spear.Math.Vector3 as Vector
11
12
13-- | A bounding volume.
14data Sphere = Sphere
15 { center :: {-# UNPACK #-} !Vector3
16 , radius :: {-# UNPACK #-} !Float
17 }
18
19
20-- | Create a 'Sphere' from the given points.
21sphere :: [Vector3] -> Sphere
22
23sphere [] = error "Attempting to build a BoundingVolume from an empty list!"
24
25sphere (x:xs) = Sphere c r
26 where
27 c = min + (max-min)/2
28 r = norm $ max - c
29 (min,max) = foldr update (x,x) xs
30 update p (min,max) = (Vector.min p min, Vector.max p max)
31
32
33-- | Return 'True' if the given 'Sphere' contains the given point, 'False' otherwise.
34spherept :: Sphere -> Vector3 -> Bool
35(Sphere center radius) `spherept` p = radius*radius >= normSq (p - center)