aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Plane.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/Math/Plane.hs')
-rw-r--r--Spear/Math/Plane.hs21
1 files changed, 13 insertions, 8 deletions
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs
index b20740c..08e4570 100644
--- a/Spear/Math/Plane.hs
+++ b/Spear/Math/Plane.hs
@@ -6,12 +6,9 @@ module Spear.Math.Plane
6) 6)
7where 7where
8 8
9
10import Spear.Math.Vector 9import Spear.Math.Vector
11 10
12 11data PointPlanePos = Front | Back | Contained deriving (Eq, Show)
13data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show)
14
15 12
16data Plane = Plane 13data Plane = Plane
17 { n :: {-# UNPACK #-} !Vector3, 14 { n :: {-# UNPACK #-} !Vector3,
@@ -19,13 +16,21 @@ data Plane = Plane
19 } 16 }
20 deriving(Eq, Show) 17 deriving(Eq, Show)
21 18
22 19-- | Construct a plane from a normal vector and a distance from the origin.
23-- | Create a plane given a normal vector and a distance from the origin.
24plane :: Vector3 -> Float -> Plane 20plane :: Vector3 -> Float -> Plane
25plane n d = Plane (normalise n) d 21plane n d = Plane (normalise n) d
26 22
27 23-- | Construct a plane from three points.
28-- | Classify the given point's relative position with respect to the given plane. 24--
25-- Points must be given in counter-clockwise order.
26fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane
27fromPoints p0 p1 p2 = Plane n d
28 where n = normalise $ v1 `cross` v2
29 v1 = p2 - p1
30 v2 = p0 - p1
31 d = p0 `dot` n
32
33-- | Classify the given point's relative position with respect to the plane.
29classify :: Plane -> Vector3 -> PointPlanePos 34classify :: Plane -> Vector3 -> PointPlanePos
30classify (Plane n d) pt = 35classify (Plane n d) pt =
31 case (n `dot` pt - d) `compare` 0 of 36 case (n `dot` pt - d) `compare` 0 of