diff options
Diffstat (limited to 'Spear/Math/Plane.hs')
| -rw-r--r-- | Spear/Math/Plane.hs | 21 |
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 | ) |
| 7 | where | 7 | where |
| 8 | 8 | ||
| 9 | |||
| 10 | import Spear.Math.Vector | 9 | import Spear.Math.Vector |
| 11 | 10 | ||
| 12 | 11 | data PointPlanePos = Front | Back | Contained deriving (Eq, Show) | |
| 13 | data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) | ||
| 14 | |||
| 15 | 12 | ||
| 16 | data Plane = Plane | 13 | data 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. | ||
| 24 | plane :: Vector3 -> Float -> Plane | 20 | plane :: Vector3 -> Float -> Plane |
| 25 | plane n d = Plane (normalise n) d | 21 | plane 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. | ||
| 26 | fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane | ||
| 27 | fromPoints 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. | ||
| 29 | classify :: Plane -> Vector3 -> PointPlanePos | 34 | classify :: Plane -> Vector3 -> PointPlanePos |
| 30 | classify (Plane n d) pt = | 35 | classify (Plane n d) pt = |
| 31 | case (n `dot` pt - d) `compare` 0 of | 36 | case (n `dot` pt - d) `compare` 0 of |
