packages feed

geomancy-0.4.0.0: test/Support/Reference/Vec3.hs

module Support.Reference.Vec3
  ( dot
  , cross
  , normalize
  ) where

import Linear (V3(..))
import Linear.Epsilon (Epsilon(..))

dot :: Num a => V3 a -> V3 a -> a
dot (V3 a b c) (V3 d e f) =
  a * d +
  b * e +
  c * f

cross :: Num a => V3 a -> V3 a -> V3 a
cross (V3 a b c) (V3 d e f) =
  V3
    (b * f - c * e)
    (c * d - a * f)
    (a * e - b * d)

normalize :: (Floating a, Epsilon a) => V3 a -> V3 a
normalize v =
  if nearZero q || nearZero (1 - q) then
    v
  else
    fmap (/ l) v
  where
    q = dot v v
    l = sqrt q