{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveLift #-}

#ifndef MIN_VERSION_hashable
#define MIN_VERSION_hashable(x,y,z) 1
#endif

#ifndef MIN_VERSION_vector
#define MIN_VERSION_vector(x,y,z) 1
#endif

#ifndef MIN_VERSION_transformers
#define MIN_VERSION_transformers(x,y,z) 1
#endif

#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif

-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2012-2015 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  experimental
-- Portability :  non-portable
--
-- 2-D Vectors
----------------------------------------------------------------------------
module Linear.V2
  ( V2(..)
  , R1(..)
  , R2(..)
  , _yx
  , ex, ey
  , perp
  , angle
  , unangle
  , crossZ
  ) where

import Control.Applicative
import Control.DeepSeq (NFData(rnf))
import Control.Monad (liftM)
import Control.Monad.Fix
import Control.Monad.Zip
import Control.Lens as Lens hiding ((<.>))
import Data.Binary as Binary
import Data.Bytes.Serial
import Data.Data
import Data.Distributive
import Data.Foldable
import qualified Data.Foldable.WithIndex as WithIndex
import Data.Functor.Bind
import Data.Functor.Classes
import Data.Functor.Rep
import qualified Data.Functor.WithIndex as WithIndex
import Data.Hashable
import Data.Hashable.Lifted
import Data.Semigroup
import Data.Semigroup.Foldable
import Data.Serialize as Cereal
import qualified Data.Traversable.WithIndex as WithIndex
import qualified Data.Vector as V
import Foreign.Ptr (castPtr)
import Foreign.Storable (Storable(..))
import GHC.Arr (Ix(..))
import GHC.Generics (Generic, Generic1)
#if defined(MIN_VERSION_template_haskell)
import Language.Haskell.TH.Syntax (Lift)
#endif
import qualified Data.Vector.Generic.Mutable as M
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed.Base as U
import Linear.Metric
import Linear.Epsilon
import Linear.V
import Linear.Vector
import Linear.V1 (R1(..),ex)
import Prelude hiding (sum)
import System.Random (Random(..))

-- $setup
-- >>> import Control.Applicative
-- >>> import Control.Lens
-- >>> import qualified Data.Foldable as F
-- >>> let sum xs = F.sum xs

-- | A 2-dimensional vector
--
-- >>> pure 1 :: V2 Int
-- V2 1 1
--
-- >>> V2 1 2 + V2 3 4
-- V2 4 6
--
-- >>> V2 1 2 * V2 3 4
-- V2 3 8
--
-- >>> sum (V2 1 2)
-- 3

data V2 a = V2 !a !a deriving
  (V2 a -> V2 a -> Bool
(V2 a -> V2 a -> Bool) -> (V2 a -> V2 a -> Bool) -> Eq (V2 a)
forall a. Eq a => V2 a -> V2 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: V2 a -> V2 a -> Bool
$c/= :: forall a. Eq a => V2 a -> V2 a -> Bool
== :: V2 a -> V2 a -> Bool
$c== :: forall a. Eq a => V2 a -> V2 a -> Bool
Eq,Eq (V2 a)
Eq (V2 a)
-> (V2 a -> V2 a -> Ordering)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> Bool)
-> (V2 a -> V2 a -> V2 a)
-> (V2 a -> V2 a -> V2 a)
-> Ord (V2 a)
V2 a -> V2 a -> Bool
V2 a -> V2 a -> Ordering
V2 a -> V2 a -> V2 a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (V2 a)
forall a. Ord a => V2 a -> V2 a -> Bool
forall a. Ord a => V2 a -> V2 a -> Ordering
forall a. Ord a => V2 a -> V2 a -> V2 a
min :: V2 a -> V2 a -> V2 a
$cmin :: forall a. Ord a => V2 a -> V2 a -> V2 a
max :: V2 a -> V2 a -> V2 a
$cmax :: forall a. Ord a => V2 a -> V2 a -> V2 a
>= :: V2 a -> V2 a -> Bool
$c>= :: forall a. Ord a => V2 a -> V2 a -> Bool
> :: V2 a -> V2 a -> Bool
$c> :: forall a. Ord a => V2 a -> V2 a -> Bool
<= :: V2 a -> V2 a -> Bool
$c<= :: forall a. Ord a => V2 a -> V2 a -> Bool
< :: V2 a -> V2 a -> Bool
$c< :: forall a. Ord a => V2 a -> V2 a -> Bool
compare :: V2 a -> V2 a -> Ordering
$ccompare :: forall a. Ord a => V2 a -> V2 a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (V2 a)
Ord,Int -> V2 a -> ShowS
[V2 a] -> ShowS
V2 a -> String
(Int -> V2 a -> ShowS)
-> (V2 a -> String) -> ([V2 a] -> ShowS) -> Show (V2 a)
forall a. Show a => Int -> V2 a -> ShowS
forall a. Show a => [V2 a] -> ShowS
forall a. Show a => V2 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [V2 a] -> ShowS
$cshowList :: forall a. Show a => [V2 a] -> ShowS
show :: V2 a -> String
$cshow :: forall a. Show a => V2 a -> String
showsPrec :: Int -> V2 a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> V2 a -> ShowS
Show,ReadPrec [V2 a]
ReadPrec (V2 a)
Int -> ReadS (V2 a)
ReadS [V2 a]
(Int -> ReadS (V2 a))
-> ReadS [V2 a]
-> ReadPrec (V2 a)
-> ReadPrec [V2 a]
-> Read (V2 a)
forall a. Read a => ReadPrec [V2 a]
forall a. Read a => ReadPrec (V2 a)
forall a. Read a => Int -> ReadS (V2 a)
forall a. Read a => ReadS [V2 a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [V2 a]
$creadListPrec :: forall a. Read a => ReadPrec [V2 a]
readPrec :: ReadPrec (V2 a)
$creadPrec :: forall a. Read a => ReadPrec (V2 a)
readList :: ReadS [V2 a]
$creadList :: forall a. Read a => ReadS [V2 a]
readsPrec :: Int -> ReadS (V2 a)
$creadsPrec :: forall a. Read a => Int -> ReadS (V2 a)
Read,Typeable (V2 a)
DataType
Constr
Typeable (V2 a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> V2 a -> c (V2 a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (V2 a))
-> (V2 a -> Constr)
-> (V2 a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (V2 a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (V2 a)))
-> ((forall b. Data b => b -> b) -> V2 a -> V2 a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r)
-> (forall u. (forall d. Data d => d -> u) -> V2 a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> V2 a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> V2 a -> m (V2 a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> V2 a -> m (V2 a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> V2 a -> m (V2 a))
-> Data (V2 a)
V2 a -> DataType
V2 a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (V2 a))
(forall b. Data b => b -> b) -> V2 a -> V2 a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> V2 a -> c (V2 a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (V2 a)
forall a. Data a => Typeable (V2 a)
forall a. Data a => V2 a -> DataType
forall a. Data a => V2 a -> Constr
forall a. Data a => (forall b. Data b => b -> b) -> V2 a -> V2 a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> V2 a -> u
forall a u. Data a => (forall d. Data d => d -> u) -> V2 a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (V2 a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> V2 a -> c (V2 a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (V2 a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (V2 a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> V2 a -> u
forall u. (forall d. Data d => d -> u) -> V2 a -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (V2 a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> V2 a -> c (V2 a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (V2 a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (V2 a))
$cV2 :: Constr
$tV2 :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
gmapMp :: (forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
gmapM :: (forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> V2 a -> m (V2 a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> V2 a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> V2 a -> u
gmapQ :: (forall d. Data d => d -> u) -> V2 a -> [u]
$cgmapQ :: forall a u. Data a => (forall d. Data d => d -> u) -> V2 a -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> V2 a -> r
gmapT :: (forall b. Data b => b -> b) -> V2 a -> V2 a
$cgmapT :: forall a. Data a => (forall b. Data b => b -> b) -> V2 a -> V2 a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (V2 a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (V2 a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (V2 a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (V2 a))
dataTypeOf :: V2 a -> DataType
$cdataTypeOf :: forall a. Data a => V2 a -> DataType
toConstr :: V2 a -> Constr
$ctoConstr :: forall a. Data a => V2 a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (V2 a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (V2 a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> V2 a -> c (V2 a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> V2 a -> c (V2 a)
$cp1Data :: forall a. Data a => Typeable (V2 a)
Data
  ,(forall x. V2 a -> Rep (V2 a) x)
-> (forall x. Rep (V2 a) x -> V2 a) -> Generic (V2 a)
forall x. Rep (V2 a) x -> V2 a
forall x. V2 a -> Rep (V2 a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (V2 a) x -> V2 a
forall a x. V2 a -> Rep (V2 a) x
$cto :: forall a x. Rep (V2 a) x -> V2 a
$cfrom :: forall a x. V2 a -> Rep (V2 a) x
Generic,(forall a. V2 a -> Rep1 V2 a)
-> (forall a. Rep1 V2 a -> V2 a) -> Generic1 V2
forall a. Rep1 V2 a -> V2 a
forall a. V2 a -> Rep1 V2 a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a. Rep1 V2 a -> V2 a
$cfrom1 :: forall a. V2 a -> Rep1 V2 a
Generic1
#if defined(MIN_VERSION_template_haskell)
  ,V2 a -> Q Exp
V2 a -> Q (TExp (V2 a))
(V2 a -> Q Exp) -> (V2 a -> Q (TExp (V2 a))) -> Lift (V2 a)
forall a. Lift a => V2 a -> Q Exp
forall a. Lift a => V2 a -> Q (TExp (V2 a))
forall t. (t -> Q Exp) -> (t -> Q (TExp t)) -> Lift t
liftTyped :: V2 a -> Q (TExp (V2 a))
$cliftTyped :: forall a. Lift a => V2 a -> Q (TExp (V2 a))
lift :: V2 a -> Q Exp
$clift :: forall a. Lift a => V2 a -> Q Exp
Lift
#endif
  )

instance Finite V2 where
  type Size V2 = 2
  toV :: V2 a -> V (Size V2) a
toV (V2 a
a a
b) = Vector a -> V 2 a
forall k (n :: k) a. Vector a -> V n a
V (Int -> [a] -> Vector a
forall a. Int -> [a] -> Vector a
V.fromListN Int
2 [a
a,a
b])
  fromV :: V (Size V2) a -> V2 a
fromV (V Vector a
v) = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
0) (Vector a
v Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
1)

instance Random a => Random (V2 a) where
  random :: g -> (V2 a, g)
random g
g = case g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g of
   (a
a, g
g') -> case g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g' of
     (a
b, g
g'') -> (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
a a
b, g
g'')
  {-# inline random #-}
  randomR :: (V2 a, V2 a) -> g -> (V2 a, g)
randomR (V2 a
a a
b, V2 a
c a
d) g
g = case (a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
a, a
c) g
g of
    (a
x, g
g') -> case (a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
b, a
d) g
g' of
      (a
y, g
g'') -> (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x a
y, g
g'')
  {-# inline randomR #-}

instance Functor V2 where
  fmap :: (a -> b) -> V2 a -> V2 b
fmap a -> b
f (V2 a
a a
b) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (a -> b
f a
a) (a -> b
f a
b)
  {-# INLINE fmap #-}
  a
a <$ :: a -> V2 b -> V2 a
<$ V2 b
_ = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
a a
a
  {-# INLINE (<$) #-}

instance Foldable V2 where
  foldMap :: (a -> m) -> V2 a -> m
foldMap a -> m
f (V2 a
a a
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` a -> m
f a
b
  {-# INLINE foldMap #-}
#if MIN_VERSION_base(4,13,0)
  foldMap' :: (a -> m) -> V2 a -> m
foldMap' a -> m
f (V2 a
a a
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` a -> m
f a
b
  {-# INLINE foldMap' #-}
#endif
  null :: V2 a -> Bool
null V2 a
_ = Bool
False
  length :: V2 a -> Int
length V2 a
_ = Int
2

instance Traversable V2 where
  traverse :: (a -> f b) -> V2 a -> f (V2 b)
traverse a -> f b
f (V2 a
a a
b) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (b -> b -> V2 b) -> f b -> f (b -> V2 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a f (b -> V2 b) -> f b -> f (V2 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
b
  {-# INLINE traverse #-}

instance Foldable1 V2 where
  foldMap1 :: (a -> m) -> V2 a -> m
foldMap1 a -> m
f (V2 a
a a
b) = a -> m
f a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
b
  {-# INLINE foldMap1 #-}

instance Traversable1 V2 where
  traverse1 :: (a -> f b) -> V2 a -> f (V2 b)
traverse1 a -> f b
f (V2 a
a a
b) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (b -> b -> V2 b) -> f b -> f (b -> V2 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a f (b -> V2 b) -> f b -> f (V2 b)
forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
<.> a -> f b
f a
b
  {-# INLINE traverse1 #-}

instance Apply V2 where
  V2 a -> b
a a -> b
b <.> :: V2 (a -> b) -> V2 a -> V2 b
<.> V2 a
d a
e = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (a -> b
a a
d) (a -> b
b a
e)
  {-# INLINE (<.>) #-}

instance Applicative V2 where
  pure :: a -> V2 a
pure a
a = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
a a
a
  {-# INLINE pure #-}
  V2 a -> b
a a -> b
b <*> :: V2 (a -> b) -> V2 a -> V2 b
<*> V2 a
d a
e = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (a -> b
a a
d) (a -> b
b a
e)
  {-# INLINE (<*>) #-}

instance Hashable a => Hashable (V2 a) where
  hashWithSalt :: Int -> V2 a -> Int
hashWithSalt Int
s (V2 a
a a
b) = Int
s Int -> a -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` a
a Int -> a -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` a
b
  {-# INLINE hashWithSalt #-}

instance Hashable1 V2 where
  liftHashWithSalt :: (Int -> a -> Int) -> Int -> V2 a -> Int
liftHashWithSalt Int -> a -> Int
h Int
s (V2 a
a a
b) = Int
s Int -> a -> Int
`h` a
a Int -> a -> Int
`h` a
b
  {-# INLINE liftHashWithSalt #-}

instance Additive V2 where
  zero :: V2 a
zero = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
0
  {-# INLINE zero #-}
  liftU2 :: (a -> a -> a) -> V2 a -> V2 a -> V2 a
liftU2 = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
  {-# INLINE liftU2 #-}
  liftI2 :: (a -> b -> c) -> V2 a -> V2 b -> V2 c
liftI2 = (a -> b -> c) -> V2 a -> V2 b -> V2 c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
  {-# INLINE liftI2 #-}

instance Bind V2 where
  V2 a
a a
b >>- :: V2 a -> (a -> V2 b) -> V2 b
>>- a -> V2 b
f = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 b
a' b
b' where
    V2 b
a' b
_ = a -> V2 b
f a
a
    V2 b
_ b
b' = a -> V2 b
f a
b
  {-# INLINE (>>-) #-}

instance Monad V2 where
#if !(MIN_VERSION_base(4,11,0))
  return a = V2 a a
  {-# INLINE return #-}
#endif
  V2 a
a a
b >>= :: V2 a -> (a -> V2 b) -> V2 b
>>= a -> V2 b
f = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 b
a' b
b' where
    V2 b
a' b
_ = a -> V2 b
f a
a
    V2 b
_ b
b' = a -> V2 b
f a
b
  {-# INLINE (>>=) #-}

instance Num a => Num (V2 a) where
  + :: V2 a -> V2 a -> V2 a
(+) = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  {-# INLINE (+) #-}
  (-) = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
  {-# INLINE (-) #-}
  * :: V2 a -> V2 a -> V2 a
(*) = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  {-# INLINE (*) #-}
  negate :: V2 a -> V2 a
negate = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
negate
  {-# INLINE negate #-}
  abs :: V2 a -> V2 a
abs = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
  {-# INLINE abs #-}
  signum :: V2 a -> V2 a
signum = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum
  {-# INLINE signum #-}
  fromInteger :: Integer -> V2 a
fromInteger = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> V2 a) -> (Integer -> a) -> Integer -> V2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
  {-# INLINE fromInteger #-}

instance Fractional a => Fractional (V2 a) where
  recip :: V2 a -> V2 a
recip = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Fractional a => a -> a
recip
  {-# INLINE recip #-}
  / :: V2 a -> V2 a -> V2 a
(/) = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
  {-# INLINE (/) #-}
  fromRational :: Rational -> V2 a
fromRational = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> V2 a) -> (Rational -> a) -> Rational -> V2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational
  {-# INLINE fromRational #-}

instance Floating a => Floating (V2 a) where
    pi :: V2 a
pi = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Floating a => a
pi
    {-# INLINE pi #-}
    exp :: V2 a -> V2 a
exp = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
exp
    {-# INLINE exp #-}
    sqrt :: V2 a -> V2 a
sqrt = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sqrt
    {-# INLINE sqrt #-}
    log :: V2 a -> V2 a
log = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
log
    {-# INLINE log #-}
    ** :: V2 a -> V2 a -> V2 a
(**) = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Floating a => a -> a -> a
(**)
    {-# INLINE (**) #-}
    logBase :: V2 a -> V2 a -> V2 a
logBase = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Floating a => a -> a -> a
logBase
    {-# INLINE logBase #-}
    sin :: V2 a -> V2 a
sin = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sin
    {-# INLINE sin #-}
    tan :: V2 a -> V2 a
tan = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
tan
    {-# INLINE tan #-}
    cos :: V2 a -> V2 a
cos = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cos
    {-# INLINE cos #-}
    asin :: V2 a -> V2 a
asin = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asin
    {-# INLINE asin #-}
    atan :: V2 a -> V2 a
atan = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atan
    {-# INLINE atan #-}
    acos :: V2 a -> V2 a
acos = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acos
    {-# INLINE acos #-}
    sinh :: V2 a -> V2 a
sinh = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
sinh
    {-# INLINE sinh #-}
    tanh :: V2 a -> V2 a
tanh = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
tanh
    {-# INLINE tanh #-}
    cosh :: V2 a -> V2 a
cosh = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
cosh
    {-# INLINE cosh #-}
    asinh :: V2 a -> V2 a
asinh = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
asinh
    {-# INLINE asinh #-}
    atanh :: V2 a -> V2 a
atanh = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
atanh
    {-# INLINE atanh #-}
    acosh :: V2 a -> V2 a
acosh = (a -> a) -> V2 a -> V2 a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Floating a => a -> a
acosh
    {-# INLINE acosh #-}

instance Metric V2 where
  dot :: V2 a -> V2 a -> a
dot (V2 a
a a
b) (V2 a
c a
d) = a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
c a -> a -> a
forall a. Num a => a -> a -> a
+ a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
d
  {-# INLINE dot #-}

-- | A space that distinguishes 2 orthogonal basis vectors '_x' and '_y', but may have more.
class R1 t => R2 t where
  -- |
  -- >>> V2 1 2 ^._y
  -- 2
  --
  -- >>> V2 1 2 & _y .~ 3
  -- V2 1 3
  --
  _y :: Lens' (t a) a
  _y = (V2 a -> f (V2 a)) -> t a -> f (t a)
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy((V2 a -> f (V2 a)) -> t a -> f (t a))
-> ((a -> f a) -> V2 a -> f (V2 a)) -> (a -> f a) -> t a -> f (t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(a -> f a) -> V2 a -> f (V2 a)
forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y
  {-# INLINE _y #-}

  _xy :: Lens' (t a) (V2 a)

-- |
-- >>> V2 1 2 ^. _yx
-- V2 2 1
_yx :: R2 t => Lens' (t a) (V2 a)
_yx :: Lens' (t a) (V2 a)
_yx V2 a -> f (V2 a)
f = (V2 a -> f (V2 a)) -> t a -> f (t a)
forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy ((V2 a -> f (V2 a)) -> t a -> f (t a))
-> (V2 a -> f (V2 a)) -> t a -> f (t a)
forall a b. (a -> b) -> a -> b
$ \(V2 a
a a
b) -> V2 a -> f (V2 a)
f (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
b a
a) f (V2 a) -> (V2 a -> V2 a) -> f (V2 a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(V2 a
b' a
a') -> a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
a' a
b'
{-# INLINE _yx #-}

ey :: R2 t => E t
ey :: E t
ey = (forall x. Lens' (t x) x) -> E t
forall (t :: * -> *). (forall x. Lens' (t x) x) -> E t
E forall x. Lens' (t x) x
forall (t :: * -> *) a. R2 t => Lens' (t a) a
_y

instance R1 V2 where
  _x :: (a -> f a) -> V2 a -> f (V2 a)
_x a -> f a
f (V2 a
a a
b) = (a -> a -> V2 a
forall a. a -> a -> V2 a
`V2` a
b) (a -> V2 a) -> f a -> f (V2 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
a
  {-# INLINE _x #-}

instance R2 V2 where
  _y :: (a -> f a) -> V2 a -> f (V2 a)
_y a -> f a
f (V2 a
a a
b) = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
a (a -> V2 a) -> f a -> f (V2 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
b
  {-# INLINE _y #-}
  _xy :: (V2 a -> f (V2 a)) -> V2 a -> f (V2 a)
_xy = (V2 a -> f (V2 a)) -> V2 a -> f (V2 a)
forall a. a -> a
id
  {-# INLINE _xy #-}

instance Distributive V2 where
  distribute :: f (V2 a) -> V2 (f a)
distribute f (V2 a)
f = f a -> f a -> V2 (f a)
forall a. a -> a -> V2 a
V2 ((V2 a -> a) -> f (V2 a) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(V2 a
x a
_) -> a
x) f (V2 a)
f) ((V2 a -> a) -> f (V2 a) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(V2 a
_ a
y) -> a
y) f (V2 a)
f)
  {-# INLINE distribute #-}

-- | the counter-clockwise perpendicular vector
--
-- >>> perp $ V2 10 20
-- V2 (-20) 10
perp :: Num a => V2 a -> V2 a
perp :: V2 a -> V2 a
perp (V2 a
a a
b) = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (a -> a
forall a. Num a => a -> a
negate a
b) a
a
{-# INLINE perp #-}

instance Epsilon a => Epsilon (V2 a) where
  nearZero :: V2 a -> Bool
nearZero = a -> Bool
forall a. Epsilon a => a -> Bool
nearZero (a -> Bool) -> (V2 a -> a) -> V2 a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. V2 a -> a
forall (f :: * -> *) a. (Metric f, Num a) => f a -> a
quadrance
  {-# INLINE nearZero #-}

instance Storable a => Storable (V2 a) where
  sizeOf :: V2 a -> Int
sizeOf V2 a
_ = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined::a)
  {-# INLINE sizeOf #-}
  alignment :: V2 a -> Int
alignment V2 a
_ = a -> Int
forall a. Storable a => a -> Int
alignment (a
forall a. HasCallStack => a
undefined::a)
  {-# INLINE alignment #-}
  poke :: Ptr (V2 a) -> V2 a -> IO ()
poke Ptr (V2 a)
ptr (V2 a
x a
y) = Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
ptr' a
x IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr a -> Int -> a -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
ptr' Int
1 a
y
    where ptr' :: Ptr a
ptr' = Ptr (V2 a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (V2 a)
ptr
  {-# INLINE poke #-}
  peek :: Ptr (V2 a) -> IO (V2 a)
peek Ptr (V2 a)
ptr = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (a -> a -> V2 a) -> IO a -> IO (a -> V2 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
ptr' IO (a -> V2 a) -> IO a -> IO (V2 a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ptr a -> Int -> IO a
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
ptr' Int
1
    where ptr' :: Ptr a
ptr' = Ptr (V2 a) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr (V2 a)
ptr
  {-# INLINE peek #-}

instance Ix a => Ix (V2 a) where
  {-# SPECIALISE instance Ix (V2 Int) #-}

  range :: (V2 a, V2 a) -> [V2 a]
range (V2 a
l1 a
l2,V2 a
u1 a
u2) =
    [ a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
i1 a
i2 | a
i1 <- (a, a) -> [a]
forall a. Ix a => (a, a) -> [a]
range (a
l1,a
u1), a
i2 <- (a, a) -> [a]
forall a. Ix a => (a, a) -> [a]
range (a
l2,a
u2) ]
  {-# INLINE range #-}

  unsafeIndex :: (V2 a, V2 a) -> V2 a -> Int
unsafeIndex (V2 a
l1 a
l2,V2 a
u1 a
u2) (V2 a
i1 a
i2) =
    (a, a) -> a -> Int
forall a. Ix a => (a, a) -> a -> Int
unsafeIndex (a
l1,a
u1) a
i1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* (a, a) -> Int
forall a. Ix a => (a, a) -> Int
unsafeRangeSize (a
l2,a
u2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (a, a) -> a -> Int
forall a. Ix a => (a, a) -> a -> Int
unsafeIndex (a
l2,a
u2) a
i2
  {-# INLINE unsafeIndex #-}

  inRange :: (V2 a, V2 a) -> V2 a -> Bool
inRange (V2 a
l1 a
l2,V2 a
u1 a
u2) (V2 a
i1 a
i2) =
    (a, a) -> a -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (a
l1,a
u1) a
i1 Bool -> Bool -> Bool
&& (a, a) -> a -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (a
l2,a
u2) a
i2
  {-# INLINE inRange #-}

instance Representable V2 where
  type Rep V2 = E V2
  tabulate :: (Rep V2 -> a) -> V2 a
tabulate Rep V2 -> a
f = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (Rep V2 -> a
f Rep V2
forall (t :: * -> *). R1 t => E t
ex) (Rep V2 -> a
f Rep V2
forall (t :: * -> *). R2 t => E t
ey)
  {-# INLINE tabulate #-}
  index :: V2 a -> Rep V2 -> a
index V2 a
xs (E l) = Getting a (V2 a) a -> V2 a -> a
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting a (V2 a) a
forall a. Lens' (V2 a) a
l V2 a
xs
  {-# INLINE index #-}

instance WithIndex.FunctorWithIndex (E V2) V2 where
  imap :: (E V2 -> a -> b) -> V2 a -> V2 b
imap E V2 -> a -> b
f (V2 a
a a
b) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (E V2 -> a -> b
f E V2
forall (t :: * -> *). R1 t => E t
ex a
a) (E V2 -> a -> b
f E V2
forall (t :: * -> *). R2 t => E t
ey a
b)
  {-# INLINE imap #-}

instance WithIndex.FoldableWithIndex (E V2) V2 where
  ifoldMap :: (E V2 -> a -> m) -> V2 a -> m
ifoldMap E V2 -> a -> m
f (V2 a
a a
b) = E V2 -> a -> m
f E V2
forall (t :: * -> *). R1 t => E t
ex a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` E V2 -> a -> m
f E V2
forall (t :: * -> *). R2 t => E t
ey a
b
  {-# INLINE ifoldMap #-}

instance WithIndex.TraversableWithIndex (E V2) V2 where
  itraverse :: (E V2 -> a -> f b) -> V2 a -> f (V2 b)
itraverse E V2 -> a -> f b
f (V2 a
a a
b) = b -> b -> V2 b
forall a. a -> a -> V2 a
V2 (b -> b -> V2 b) -> f b -> f (b -> V2 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> E V2 -> a -> f b
f E V2
forall (t :: * -> *). R1 t => E t
ex a
a f (b -> V2 b) -> f b -> f (V2 b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> E V2 -> a -> f b
f E V2
forall (t :: * -> *). R2 t => E t
ey a
b
  {-# INLINE itraverse #-}

#if !MIN_VERSION_lens(5,0,0)
instance Lens.FunctorWithIndex     (E V2) V2 where imap      = WithIndex.imap
instance Lens.FoldableWithIndex    (E V2) V2 where ifoldMap  = WithIndex.ifoldMap
instance Lens.TraversableWithIndex (E V2) V2 where itraverse = WithIndex.itraverse
#endif

type instance Index (V2 a) = E V2
type instance IxValue (V2 a) = a

instance Ixed (V2 a) where
  ix :: Index (V2 a) -> Traversal' (V2 a) (IxValue (V2 a))
ix Index (V2 a)
i = E V2 -> forall a. Lens' (V2 a) a
forall (t :: * -> *). E t -> forall x. Lens' (t x) x
el Index (V2 a)
E V2
i
  {-# INLINE ix #-}

instance Each (V2 a) (V2 b) a b where
  each :: (a -> f b) -> V2 a -> f (V2 b)
each = (a -> f b) -> V2 a -> f (V2 b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
  {-# INLINE each #-}

data instance U.Vector    (V2 a) =  V_V2 {-# UNPACK #-} !Int !(U.Vector    a)
data instance U.MVector s (V2 a) = MV_V2 {-# UNPACK #-} !Int !(U.MVector s a)
instance U.Unbox a => U.Unbox (V2 a)

instance U.Unbox a => M.MVector U.MVector (V2 a) where
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicOverlaps #-}
  {-# INLINE basicUnsafeNew #-}
  {-# INLINE basicUnsafeRead #-}
  {-# INLINE basicUnsafeWrite #-}
  basicLength :: MVector s (V2 a) -> Int
basicLength (MV_V2 n _) = Int
n
  basicUnsafeSlice :: Int -> Int -> MVector s (V2 a) -> MVector s (V2 a)
basicUnsafeSlice Int
m Int
n (MV_V2 _ v) = Int -> MVector s a -> MVector s (V2 a)
forall s a. Int -> MVector s a -> MVector s (V2 a)
MV_V2 Int
n (Int -> Int -> MVector s a -> MVector s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
M.basicUnsafeSlice (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
m) (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) MVector s a
v)
  basicOverlaps :: MVector s (V2 a) -> MVector s (V2 a) -> Bool
basicOverlaps (MV_V2 _ v) (MV_V2 _ u) = MVector s a -> MVector s a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
M.basicOverlaps MVector s a
v MVector s a
u
  basicUnsafeNew :: Int -> m (MVector (PrimState m) (V2 a))
basicUnsafeNew Int
n = (MVector (PrimState m) a -> MVector (PrimState m) (V2 a))
-> m (MVector (PrimState m) a) -> m (MVector (PrimState m) (V2 a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Int -> MVector (PrimState m) a -> MVector (PrimState m) (V2 a)
forall s a. Int -> MVector s a -> MVector s (V2 a)
MV_V2 Int
n) (Int -> m (MVector (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
M.basicUnsafeNew (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n))
  basicUnsafeRead :: MVector (PrimState m) (V2 a) -> Int -> m (V2 a)
basicUnsafeRead (MV_V2 _ v) Int
i =
    do let o :: Int
o = Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i
       a
x <- MVector (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
M.basicUnsafeRead MVector (PrimState m) a
v Int
o
       a
y <- MVector (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
M.basicUnsafeRead MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       V2 a -> m (V2 a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x a
y)
  basicUnsafeWrite :: MVector (PrimState m) (V2 a) -> Int -> V2 a -> m ()
basicUnsafeWrite (MV_V2 _ v) Int
i (V2 a
x a
y) =
    do let o :: Int
o = Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i
       MVector (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
M.basicUnsafeWrite MVector (PrimState m) a
v Int
o     a
x
       MVector (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
M.basicUnsafeWrite MVector (PrimState m) a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
y
  basicInitialize :: MVector (PrimState m) (V2 a) -> m ()
basicInitialize (MV_V2 _ v) = MVector (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
M.basicInitialize MVector (PrimState m) a
v
  {-# INLINE basicInitialize #-}

instance U.Unbox a => G.Vector U.Vector (V2 a) where
  {-# INLINE basicUnsafeFreeze #-}
  {-# INLINE basicUnsafeThaw   #-}
  {-# INLINE basicLength       #-}
  {-# INLINE basicUnsafeSlice  #-}
  {-# INLINE basicUnsafeIndexM #-}
  basicUnsafeFreeze :: Mutable Vector (PrimState m) (V2 a) -> m (Vector (V2 a))
basicUnsafeFreeze (MV_V2 n v) = (Vector a -> Vector (V2 a)) -> m (Vector a) -> m (Vector (V2 a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ( Int -> Vector a -> Vector (V2 a)
forall a. Int -> Vector a -> Vector (V2 a)
V_V2 Int
n) (Mutable Vector (PrimState m) a -> m (Vector a)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
G.basicUnsafeFreeze MVector (PrimState m) a
Mutable Vector (PrimState m) a
v)
  basicUnsafeThaw :: Vector (V2 a) -> m (Mutable Vector (PrimState m) (V2 a))
basicUnsafeThaw   ( V_V2 n v) = (MVector (PrimState m) a -> MVector (PrimState m) (V2 a))
-> m (MVector (PrimState m) a) -> m (MVector (PrimState m) (V2 a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Int -> MVector (PrimState m) a -> MVector (PrimState m) (V2 a)
forall s a. Int -> MVector s a -> MVector s (V2 a)
MV_V2 Int
n) (Vector a -> m (Mutable Vector (PrimState m) a)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
G.basicUnsafeThaw   Vector a
v)
  basicLength :: Vector (V2 a) -> Int
basicLength       ( V_V2 n _) = Int
n
  basicUnsafeSlice :: Int -> Int -> Vector (V2 a) -> Vector (V2 a)
basicUnsafeSlice Int
m Int
n (V_V2 _ v) = Int -> Vector a -> Vector (V2 a)
forall a. Int -> Vector a -> Vector (V2 a)
V_V2 Int
n (Int -> Int -> Vector a -> Vector a
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
G.basicUnsafeSlice (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
m) (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n) Vector a
v)
  basicUnsafeIndexM :: Vector (V2 a) -> Int -> m (V2 a)
basicUnsafeIndexM (V_V2 _ v) Int
i =
    do let o :: Int
o = Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i
       a
x <- Vector a -> Int -> m a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.basicUnsafeIndexM Vector a
v Int
o
       a
y <- Vector a -> Int -> m a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> Int -> m a
G.basicUnsafeIndexM Vector a
v (Int
oInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
       V2 a -> m (V2 a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x a
y)

instance MonadZip V2 where
  mzipWith :: (a -> b -> c) -> V2 a -> V2 b -> V2 c
mzipWith = (a -> b -> c) -> V2 a -> V2 b -> V2 c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2

instance MonadFix V2 where
  mfix :: (a -> V2 a) -> V2 a
mfix a -> V2 a
f = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (let V2 a
a a
_ = a -> V2 a
f a
a in a
a)
              (let V2 a
_ a
a = a -> V2 a
f a
a in a
a)

angle :: Floating a => a -> V2 a
angle :: a -> V2 a
angle a
a = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (a -> a
forall a. Floating a => a -> a
cos a
a) (a -> a
forall a. Floating a => a -> a
sin a
a)

unangle :: (Floating a, Ord a) => V2 a -> a
unangle :: V2 a -> a
unangle a :: V2 a
a@(V2 a
ax a
ay) =
  let alpha :: a
alpha = a -> a
forall a. Floating a => a -> a
asin (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
$ a
ay a -> a -> a
forall a. Fractional a => a -> a -> a
/ V2 a -> a
forall (f :: * -> *) a. (Metric f, Floating a) => f a -> a
norm V2 a
a
  in if a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0
       then a
forall a. Floating a => a
pi a -> a -> a
forall a. Num a => a -> a -> a
- a
alpha
       else a
alpha

-- | The Z-component of the cross product of two vectors in the XY-plane.
--
-- >>> crossZ (V2 1 0) (V2 0 1)
-- 1
crossZ :: Num a => V2 a -> V2 a -> a
crossZ :: V2 a -> V2 a -> a
crossZ (V2 a
x1 a
y1) (V2 a
x2 a
y2) = a
x1a -> a -> a
forall a. Num a => a -> a -> a
*a
y2 a -> a -> a
forall a. Num a => a -> a -> a
- a
y1a -> a -> a
forall a. Num a => a -> a -> a
*a
x2
{-# INLINE crossZ #-}

instance Bounded a => Bounded (V2 a) where
  minBound :: V2 a
minBound = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Bounded a => a
minBound
  {-# INLINE minBound #-}
  maxBound :: V2 a
maxBound = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Bounded a => a
maxBound
  {-# INLINE maxBound #-}

instance NFData a => NFData (V2 a) where
  rnf :: V2 a -> ()
rnf (V2 a
a a
b) = a -> ()
forall a. NFData a => a -> ()
rnf a
a () -> () -> ()
`seq` a -> ()
forall a. NFData a => a -> ()
rnf a
b

instance Serial1 V2 where
  serializeWith :: (a -> m ()) -> V2 a -> m ()
serializeWith = (a -> m ()) -> V2 a -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_
  deserializeWith :: m a -> m (V2 a)
deserializeWith m a
k = a -> a -> V2 a
forall a. a -> a -> V2 a
V2 (a -> a -> V2 a) -> m a -> m (a -> V2 a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
k m (a -> V2 a) -> m a -> m (V2 a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> m a
k

instance Serial a => Serial (V2 a) where
  serialize :: V2 a -> m ()
serialize = (a -> m ()) -> V2 a -> m ()
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> m ()
forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
  deserialize :: m (V2 a)
deserialize = m a -> m (V2 a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith m a
forall a (m :: * -> *). (Serial a, MonadGet m) => m a
deserialize

instance Binary a => Binary (V2 a) where
  put :: V2 a -> Put
put = (a -> Put) -> V2 a -> Put
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> Put
forall t. Binary t => t -> Put
Binary.put
  get :: Get (V2 a)
get = Get a -> Get (V2 a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith Get a
forall t. Binary t => Get t
Binary.get

instance Serialize a => Serialize (V2 a) where
  put :: Putter (V2 a)
put = (a -> PutM ()) -> Putter (V2 a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadPut m) =>
(a -> m ()) -> f a -> m ()
serializeWith a -> PutM ()
forall t. Serialize t => Putter t
Cereal.put
  get :: Get (V2 a)
get = Get a -> Get (V2 a)
forall (f :: * -> *) (m :: * -> *) a.
(Serial1 f, MonadGet m) =>
m a -> m (f a)
deserializeWith Get a
forall t. Serialize t => Get t
Cereal.get

instance Eq1 V2 where
  liftEq :: (a -> b -> Bool) -> V2 a -> V2 b -> Bool
liftEq a -> b -> Bool
f (V2 a
a a
b) (V2 b
c b
d) = a -> b -> Bool
f a
a b
c Bool -> Bool -> Bool
&& a -> b -> Bool
f a
b b
d
instance Ord1 V2 where
  liftCompare :: (a -> b -> Ordering) -> V2 a -> V2 b -> Ordering
liftCompare a -> b -> Ordering
f (V2 a
a a
b) (V2 b
c b
d) = a -> b -> Ordering
f a
a b
c Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend` a -> b -> Ordering
f a
b b
d
instance Read1 V2 where
  liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (V2 a)
liftReadsPrec Int -> ReadS a
f ReadS [a]
_ = (String -> ReadS (V2 a)) -> Int -> ReadS (V2 a)
forall a. (String -> ReadS a) -> Int -> ReadS a
readsData ((String -> ReadS (V2 a)) -> Int -> ReadS (V2 a))
-> (String -> ReadS (V2 a)) -> Int -> ReadS (V2 a)
forall a b. (a -> b) -> a -> b
$ (Int -> ReadS a)
-> (Int -> ReadS a)
-> String
-> (a -> a -> V2 a)
-> String
-> ReadS (V2 a)
forall a b t.
(Int -> ReadS a)
-> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t
readsBinaryWith Int -> ReadS a
f Int -> ReadS a
f String
"V2" a -> a -> V2 a
forall a. a -> a -> V2 a
V2
instance Show1 V2 where
  liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> V2 a -> ShowS
liftShowsPrec Int -> a -> ShowS
f [a] -> ShowS
_ Int
d (V2 a
a a
b) = (Int -> a -> ShowS)
-> (Int -> a -> ShowS) -> String -> Int -> a -> a -> ShowS
forall a b.
(Int -> a -> ShowS)
-> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS
showsBinaryWith Int -> a -> ShowS
f Int -> a -> ShowS
f String
"V2" Int
d a
a a
b

instance Field1 (V2 a) (V2 a) a a where
  _1 :: (a -> f a) -> V2 a -> f (V2 a)
_1 a -> f a
f (V2 a
x a
y) = a -> f a
f a
x f a -> (a -> V2 a) -> f (V2 a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
x' -> a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x' a
y

instance Field2 (V2 a) (V2 a) a a where
  _2 :: (a -> f a) -> V2 a -> f (V2 a)
_2 a -> f a
f (V2 a
x a
y) = a -> f a
f a
y f a -> (a -> V2 a) -> f (V2 a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
y' -> a -> a -> V2 a
forall a. a -> a -> V2 a
V2 a
x a
y'

instance Semigroup a => Semigroup (V2 a) where
 <> :: V2 a -> V2 a -> V2 a
(<>) = (a -> a -> a) -> V2 a -> V2 a -> V2 a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)

instance Monoid a => Monoid (V2 a) where
  mempty :: V2 a
mempty = a -> V2 a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
#if !(MIN_VERSION_base(4,11,0))
  mappend = liftA2 mappend
#endif