module Blubber.Server.Vector where
import Data.Serialize
(
Serialize,
)
import GHC.Generics
(
Generic,
)
data Vector = Vec
{vx :: Double
,vy :: Double
} deriving (Eq, Generic, Show)
instance Serialize Vector
infixl 6 ^-^
(^-^) :: Vector -> Vector -> Vector
(Vec x y) ^-^ (Vec x' y') = Vec (x x') (y y')
infixl 6 ^+^
(^+^) :: Vector -> Vector -> Vector
(Vec x y) ^+^ (Vec x' y') = Vec (x + x') (y + y')
infixl 7 ^*^
(^*^) :: Vector -> Double -> Vector
(Vec x y) ^*^ n = Vec (n * x) (n * y)
infixl 7 ^/^
(^/^) :: Vector -> Double -> Vector
(Vec x y) ^/^ n = Vec (n / x) (n / y)
magVec :: Vector -> Double
magVec (Vec x y) = sqrt (x * x + y * y)
vecLimitMag :: Double -> Vector -> Vector
vecLimitMag m v | magVec v > m = v ^*^ (m / magVec v)
| otherwise = v