-- | Description: Math helpers.
-- Module provides mathematics helper functions.
module Chiphunk.Low.Math where

import Data.VectorSpace

-- | Clamp @f@ to be between @min@ and @max@
fClamp
  :: Double -- ^ f
  -> Double -- ^ min
  -> Double -- ^ max
  -> Double
fClamp :: Double -> Double -> Double -> Double
fClamp Double
x Double
a Double
b
  | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
a     = Double
a
  | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
b     = Double
b
  | Bool
otherwise = Double
x

-- | Linearly interpolate between @f1@ and @f2@
fLerp
  :: Double -- ^ f1
  -> Double -- ^ f2
  -> Double
  -> Double
fLerp :: Double -> Double -> Double -> Double
fLerp = Double -> Double -> Double -> Double
forall v. VectorSpace v => v -> v -> Scalar v -> v
lerp

-- | Linearly interpolate from @f1@ towards @f2@ by no more than @d@.
fLerpConst
  :: Double -- ^ f1
  -> Double -- ^ f2
  -> Double -- ^ d
  -> Double
fLerpConst :: Double -> Double -> Double -> Double
fLerpConst Double
x Double
y Double
d = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double -> Double -> Double
fClamp (Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
x) (-Double
d) Double
d