diff options
Diffstat (limited to 'Spear/Math/Plane.hs')
-rw-r--r-- | Spear/Math/Plane.hs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs new file mode 100644 index 0000000..0f5829b --- /dev/null +++ b/Spear/Math/Plane.hs | |||
@@ -0,0 +1,33 @@ | |||
1 | module Spear.Math.Plane | ||
2 | ( | ||
3 | Plane | ||
4 | , plane | ||
5 | , classify | ||
6 | ) | ||
7 | where | ||
8 | |||
9 | |||
10 | import Spear.Math.Vector3 as Vector | ||
11 | |||
12 | |||
13 | data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) | ||
14 | |||
15 | |||
16 | data Plane = Plane { | ||
17 | n :: !Vector3, | ||
18 | d :: !Float | ||
19 | } deriving(Eq, Show) | ||
20 | |||
21 | |||
22 | -- | Create a plane given a normal vector and a distance from the origin. | ||
23 | plane :: Vector3 -> Float -> Plane | ||
24 | plane n d = Plane (normalise n) d | ||
25 | |||
26 | |||
27 | -- | Classify the given point's relative position with respect to the given plane. | ||
28 | classify :: Plane -> Vector3 -> PointPlanePos | ||
29 | classify (Plane n d) pt = case (n `dot` pt - d) `compare` 0 of | ||
30 | GT -> Front | ||
31 | LT -> Back | ||
32 | EQ -> Contained | ||
33 | \ No newline at end of file | ||