-- | Miscellaneous utils for math
module GIS.Math.Utils ( radians
                      , toRadians
                      , sinc
                      , avg
                      , shittyCentroid
                      ) where

import           Control.Composition
import           GIS.Types

-- | Averages the coördinates of a polygon, returning a point.
shittyCentroid :: Polygon -> Point
shittyCentroid poly = (avg $ fmap fst poly, avg $ fmap snd poly)

-- | Average over a foldable container
avg :: (RealFrac a, Foldable t) => t a -> a
avg list = sum list / (fromIntegral . length $ list)

sinc :: Floating a => a -> a
sinc x = sin x / x

-- | Convert a `Double` from degrees to radians.
radians :: Double -> Double
radians = (*(pi/180))

-- | Convert both coördinates to radians.
toRadians :: Point -> Point
toRadians = both radians