packages feed

mischief-math-0.1.0.0: src/Mischief/Math/Vec.hs

module Mischief.Math.Vec
  ( V2 (..),
    V3 (..),
    V4 (..),
    ext2,
    trunc3,
    Dir3 (Dir3),
    dir3,
  )
where

import GHC.Records
import Linear

ext2 :: a -> V2 a -> V3 a
ext2 z (V2 x y) = V3 x y z

trunc3 :: V3 a -> V2 a
trunc3 (V3 x y _) = V2 x y

newtype Dir3 a = Dir3 (V3 a) deriving newtype (Show, Eq, Ord)

instance (Floating a, Epsilon a) => Num (Dir3 a) where
  (+) (Dir3 a) (Dir3 b) = Dir3 (normalize $ a + b)
  (*) (Dir3 a) (Dir3 b) = Dir3 (normalize $ a - b)
  abs (Dir3 a) = Dir3 (abs a)
  signum (Dir3 a) = Dir3 (signum a)
  fromInteger = Dir3 . fromInteger
  negate (Dir3 a) = Dir3 (negate a)

instance HasField "vec" (Dir3 a) (V3 a) where
  getField :: Dir3 a -> V3 a
  getField (Dir3 v3) = v3

dir3 :: (Floating a, Epsilon a) => a -> a -> a -> Dir3 a
dir3 x y z = Dir3 $ normalize $ V3 x y z