Mid strictifying

This commit is contained in:
jgk
2021-07-29 23:42:27 +02:00
parent 67aa5c05c7
commit bd8ef3f416
22 changed files with 359 additions and 325 deletions
+28 -27
View File
@@ -5,7 +5,7 @@ import Geometry.Data
{- | Moves from two to three dimensions, adding zero in z direction. -}
zeroZ :: Point2 -> Point3
{-# INLINE zeroZ #-}
zeroZ (x,y) = (x,y,0)
zeroZ (V2 x y) = V3 x y 0
infixl 6 +.+, -.-
infixl 7 *.*
@@ -13,27 +13,29 @@ infixl 7 *.*
{- | 2D coordinate-wise addition. -}
(+.+) :: Point2 -> Point2 -> Point2
{-# INLINE (+.+) #-}
(x1, y1) +.+ (x2, y2) =
let
!x = x1 + x2
!y = y1 + y2
in (x, y)
(+.+) = (+)
--(x1, y1) +.+ (x2, y2) =
-- let
-- !x = x1 + x2
-- !y = y1 + y2
-- in (x, y)
{- | 2D coordinate-wise subtraction. -}
(-.-) :: Point2 -> Point2 -> Point2
{-# INLINE (-.-) #-}
(x1, y1) -.- (x2, y2) =
let
!x = x1 - x2
!y = y1 - y2
in (x, y)
(-.-) = (-)
--(x1, y1) -.- (x2, y2) =
-- let
-- !x = x1 - x2
-- !y = y1 - y2
-- in (x, y)
{- | 2D scalar multiplication. -}
(*.*) :: Float -> Point2 -> Point2
{-# INLINE (*.*) #-}
a *.* (x2, y2) =
a *.* V2 x2 y2 =
let
!x = a * x2
!y = a * y2
in (x, y)
in V2 x y
{- | Normalize a vector to length 1. -}
normalizeV :: Point2 -> Point2
@@ -53,31 +55,30 @@ angleVV a b
safeAngleVV :: Point2 -> Point2 -> Float
{-# INLINE safeAngleVV #-}
safeAngleVV a b
| a == (0,0) || b == (0,0) = 0
| a == V2 0 0 || b == V2 0 0 = 0
| otherwise = angleVV a b
{- | Dot product. -}
dotV :: Point2 -> Point2 -> Float
{-# INLINE dotV #-}
dotV (x,y) (z,w) = x*z + y*w
dotV (V2 x y) (V2 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
argV (V2 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
detV (V2 x1 y1) (V2 x2 y2) = x1 * y2 - y1 * x2
{- | Given an angle in radians, anticlockwise from +ve x-axis,
- returns the corresponding unit vector. -}
unitVectorAtAngle :: Float -> Point2
{-# INLINE unitVectorAtAngle #-}
unitVectorAtAngle r = (cos r, sin r)
unitVectorAtAngle r = V2 (cos r) (sin r)
-- | Rotate a vector by an angle (in radians). +ve angle is counter-clockwise.
rotateV :: Float -> Point2 -> Point2
rotateV r (x, y) =
( x * cos r - y * sin r
, x * sin r + y * cos r
)
rotateV r (V2 x y) = V2
(x * cos r - y * sin r)
(x * sin r + y * cos r)
{-# INLINE rotateV #-}
-- | Convert degrees to radians
degToRad :: Float -> Float
@@ -97,23 +98,23 @@ normalizeAngle f = f - 2 * pi * floor' (f / (2 * pi))
{- | Rotate vector by pi/2 clockwise. -}
vNormal :: Point2 -> Point2
{-# INLINE vNormal #-}
vNormal (x,y) = (y,-x)
vNormal (V2 x y) = V2 y (negate x)
{- | Negate a vector. -}
vInverse :: Point2 -> Point2
vInverse (x,y) = (-x,-y)
vInverse (V2 x y) = V2 (-x) (-y)
{- | Normalize a vector safely: on (0,0) return (0,0). -}
safeNormalizeV :: Point2 -> Point2
{-# INLINE safeNormalizeV #-}
safeNormalizeV (0,0) = (0,0)
safeNormalizeV (V2 0 0) = V2 0 0
safeNormalizeV p = (1/magV p ) *.* p
{- | Magnitude of a vector. -}
magV :: Point2 -> Float
{-# INLINE magV #-}
magV (x,y) = sqrt $ x^(2::Int) + y^(2::Int)
magV (V2 x y) = sqrt $ x^(2::Int) + y^(2::Int)
{- | Magnitude of the cross product of two vectors.
Identical to detV. -}
crossV :: Point2 -> Point2 -> Float
crossV (ax,ay) (bx,by) = ax*by - ay*bx
crossV (V2 ax ay) (V2 bx by) = ax*by - ay*bx
{- | TO CHECK Orthographic projection of one vector onto another. -}
projV :: Point2 -> Point2 -> Point2
projV fromv onv