Add haddocks

This commit is contained in:
jgk
2021-04-04 18:53:43 +02:00
parent d3821fc7b3
commit b3649597fa
5 changed files with 61 additions and 21 deletions
+48 -13
View File
@@ -2,7 +2,8 @@
module Geometry.Vector
where
import Geometry.Data
{- | Moves from to three dimensions, adding zero in z direction.
-}
zeroZ :: Point2 -> Point3
{-# INLINE zeroZ #-}
zeroZ (x,y) = (x,y,0)
@@ -10,6 +11,8 @@ zeroZ (x,y) = (x,y,0)
infixl 6 +.+, -.-
infixl 7 *.*
{- | 2D coordinate-wise addition.
-}
(+.+) :: Point2 -> Point2 -> Point2
{-# INLINE (+.+) #-}
(x1, y1) +.+ (x2, y2) =
@@ -17,7 +20,8 @@ infixl 7 *.*
!x = x1 + x2
!y = y1 + y2
in (x, y)
{- | 2D coordinate-wise subtraction.
-}
(-.-) :: Point2 -> Point2 -> Point2
{-# INLINE (-.-) #-}
(x1, y1) -.- (x2, y2) =
@@ -25,7 +29,8 @@ infixl 7 *.*
!x = x1 - x2
!y = y1 - y2
in (x, y)
{- | 2D scalar multiplication.
-}
(*.*) :: Float -> Point2 -> Point2
{-# INLINE (*.*) #-}
a *.* (x2, y2) =
@@ -37,6 +42,9 @@ a *.* (x2, y2) =
infixl 6 +.+.+, -.-.-
infixl 7 *.*.*
{- | 3D coordinate-wise addition.
-}
(+.+.+) :: Point3 -> Point3 -> Point3
{-# INLINE (+.+.+) #-}
(x1, y1, z1) +.+.+ (x2, y2, z2) =
@@ -46,6 +54,8 @@ infixl 7 *.*.*
!z = z1 + z2
in (x, y, z)
{- | 3D coordinate-wise subtraction.
-}
(-.-.-) :: Point3 -> Point3 -> Point3
{-# INLINE (-.-.-) #-}
(x1, y1, z1) -.-.- (x2, y2, z2) =
@@ -55,6 +65,8 @@ infixl 7 *.*.*
!z = z1 - z2
in (x, y, z)
{- | 3D scalar multiplication.
-}
(*.*.*) :: Point3 -> Point3 -> Point3
{-# INLINE (*.*.*) #-}
(x1, y1, z1) *.*.* (x2, y2, z2) =
@@ -64,10 +76,15 @@ infixl 7 *.*.*
!z = z1 * z2
in (x, y, z)
{- | Normalize a vector to length 1.
-}
normalizeV :: Point2 -> Point2
{-# INLINE normalizeV #-}
normalizeV p = (1 / magV p) *.* p
{- | Angle between two vectors.
Always positive.
-}
angleVV :: Point2 -> Point2 -> Float
{-# INLINE angleVV #-}
angleVV a b = let ma = magV a
@@ -75,20 +92,28 @@ angleVV a b = let ma = magV a
d = a `dotV` b
in acos $ d / (ma * mb)
{- | Dot product.
-}
dotV :: Point2 -> Point2 -> Float
{-# INLINE dotV #-}
dotV (x,y) (z,w) = x*z + y*w
{- | Given vector, returns the angle, anticlockwise from +ve x-axis, in radians.
-}
argV :: Point2 -> Float
{-# INLINE argV #-}
argV (x,y) = normalizeAngle $ atan2 y x
{- | Determinant of the matrix formed by two vectors.
-}
detV :: Point2 -> Point2 -> Float
{-# INLINE detV #-}
detV (x1, y1) (x2, y2)
= x1 * y2 - y1 * x2
-- | Angle in radians, anticlockwise from +ve x-axis.
{- | Given an angle in radians, anticlockwise from +ve x-axis, returns the
corresponding unit vector.
-}
unitVectorAtAngle :: Float -> Point2
{-# INLINE unitVectorAtAngle #-}
unitVectorAtAngle r
@@ -106,36 +131,46 @@ degToRad :: Float -> Float
degToRad d = d * pi / 180
{-# INLINE degToRad #-}
-- | Convert radians to degrees
radToDeg :: Float -> Float
radToDeg r = r * 180 / pi
{-# INLINE radToDeg #-}
-- | Normalize an angle to be between 0 and 2*pi radians
normalizeAngle :: Float -> Float
normalizeAngle f = f - 2 * pi * floor' (f / (2 * pi))
where floor' :: Float -> Float
floor' x = fromIntegral (floor x :: Int)
{-# INLINE normalizeAngle #-}
normalizeAngle f = f - 2 * pi * floor' (f / (2 * pi))
where
floor' :: Float -> Float
floor' x = fromIntegral (floor x :: Int)
{- | Rotate vector by pi/2 clockwise.
-}
vNormal :: Point2 -> Point2
{-# INLINE vNormal #-}
vNormal (x,y) = (y,-x)
{- | Negate a vector.
-}
vInverse :: Point2 -> Point2
vInverse (x,y) = (-x,-y)
normV :: Point2 -> Point2
{-# INLINE normV #-}
normV (0,0) = (0,0)
normV p = (1/magV p ) *.* p
{- | Normalize a vector safely: on (0,0) return (0,0).
-}
safeNormalizeV :: Point2 -> Point2
{-# INLINE safeNormalizeV #-}
safeNormalizeV (0,0) = (0,0)
safeNormalizeV p = (1/magV p ) *.* p
{- | Magnitude of a vector.
-}
magV :: Point2 -> Float
{-# INLINE magV #-}
magV (x,y) = sqrt $ x^2 + y^2
{- | Magnitude of the cross product of two vectors.
Identical to detV.
-}
crossV :: Point2 -> Point2 -> Float
crossV (ax,ay) (bx,by) = ax*by - ay*bx