{-# 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
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,V2 a -> V2 a -> Bool
V2 a -> V2 a -> Ordering
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
Ord,Int -> V2 a -> ShowS
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)
ReadS [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,V2 a -> DataType
V2 a -> Constr
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 (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))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u. 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 u. (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 :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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)
Data
  ,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. 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)
  ,forall a (m :: * -> *). (Lift a, Quote m) => V2 a -> m Exp
forall a (m :: * -> *). (Lift a, Quote m) => V2 a -> Code m (V2 a)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => V2 a -> m Exp
forall (m :: * -> *). Quote m => V2 a -> Code m (V2 a)
liftTyped :: forall (m :: * -> *). Quote m => V2 a -> Code m (V2 a)
$cliftTyped :: forall a (m :: * -> *). (Lift a, Quote m) => V2 a -> Code m (V2 a)
lift :: forall (m :: * -> *). Quote m => V2 a -> m Exp
$clift :: forall a (m :: * -> *). (Lift a, Quote m) => V2 a -> m Exp
Lift
#endif
  )

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

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

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

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

instance Traversable V2 where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> V2 a -> f (V2 b)
traverse a -> f b
f (V2 a
a a
b) = forall a. a -> a -> V2 a
V2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a 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 :: forall m a. Semigroup m => (a -> m) -> V2 a -> m
foldMap1 a -> m
f (V2 a
a a
b) = a -> m
f a
a forall a. Semigroup a => a -> a -> a
<> a -> m
f a
b
  {-# INLINE foldMap1 #-}

instance Traversable1 V2 where
  traverse1 :: forall (f :: * -> *) a b. Apply f => (a -> f b) -> V2 a -> f (V2 b)
traverse1 a -> f b
f (V2 a
a a
b) = forall a. a -> a -> V2 a
V2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a 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 <.> :: forall a b. V2 (a -> b) -> V2 a -> V2 b
<.> V2 a
d a
e = forall a. a -> a -> V2 a
V2 (a -> b
a a
d) (a -> b
b a
e)
  {-# INLINE (<.>) #-}

instance Applicative V2 where
  pure :: forall a. a -> V2 a
pure a
a = forall a. a -> a -> V2 a
V2 a
a a
a
  {-# INLINE pure #-}
  V2 a -> b
a a -> b
b <*> :: forall a b. V2 (a -> b) -> V2 a -> V2 b
<*> V2 a
d a
e = 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 forall a. Hashable a => Int -> a -> Int
`hashWithSalt` a
a forall a. Hashable a => Int -> a -> Int
`hashWithSalt` a
b
  {-# INLINE hashWithSalt #-}

instance Hashable1 V2 where
  liftHashWithSalt :: forall a. (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 :: forall a. Num a => V2 a
zero = forall (f :: * -> *) a. Applicative f => a -> f a
pure a
0
  {-# INLINE zero #-}
  liftU2 :: forall a. (a -> a -> a) -> V2 a -> V2 a -> V2 a
liftU2 = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2
  {-# INLINE liftU2 #-}
  liftI2 :: forall a b c. (a -> b -> c) -> V2 a -> V2 b -> V2 c
liftI2 = 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 >>- :: forall a b. V2 a -> (a -> V2 b) -> V2 b
>>- a -> V2 b
f = 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 >>= :: forall a b. V2 a -> (a -> V2 b) -> V2 b
>>= a -> V2 b
f = 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
(+) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Num a => a -> a -> a
(+)
  {-# INLINE (+) #-}
  (-) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
  {-# INLINE (-) #-}
  * :: V2 a -> V2 a -> V2 a
(*) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Num a => a -> a -> a
(*)
  {-# INLINE (*) #-}
  negate :: V2 a -> V2 a
negate = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
negate
  {-# INLINE negate #-}
  abs :: V2 a -> V2 a
abs = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
abs
  {-# INLINE abs #-}
  signum :: V2 a -> V2 a
signum = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
signum
  {-# INLINE signum #-}
  fromInteger :: Integer -> V2 a
fromInteger = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
  {-# INLINE fromInteger #-}

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

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

instance Metric V2 where
  dot :: forall a. Num a => V2 a -> V2 a -> a
dot (V2 a
a a
b) (V2 a
c a
d) = a
a forall a. Num a => a -> a -> a
* a
c forall a. Num a => a -> a -> a
+ a
b 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 = forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xyforall b c a. (b -> c) -> (a -> b) -> a -> c
.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 :: forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_yx V2 a -> f (V2 a)
f = forall (t :: * -> *) a. R2 t => Lens' (t a) (V2 a)
_xy forall a b. (a -> b) -> a -> b
$ \(V2 a
a a
b) -> V2 a -> f (V2 a)
f (forall a. a -> a -> V2 a
V2 a
b a
a) forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(V2 a
b' a
a') -> forall a. a -> a -> V2 a
V2 a
a' a
b'
{-# INLINE _yx #-}

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

instance R1 V2 where
  _x :: forall a. Lens' (V2 a) a
_x a -> f a
f (V2 a
a a
b) = (forall a. a -> a -> V2 a
`V2` a
b) 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 :: forall a. Lens' (V2 a) a
_y a -> f a
f (V2 a
a a
b) = forall a. a -> a -> V2 a
V2 a
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f a
f a
b
  {-# INLINE _y #-}
  _xy :: forall a. Lens' (V2 a) (V2 a)
_xy = forall a. a -> a
id
  {-# INLINE _xy #-}

instance Distributive V2 where
  distribute :: forall (f :: * -> *) a. Functor f => f (V2 a) -> V2 (f a)
distribute f (V2 a)
f = forall a. a -> a -> V2 a
V2 (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(V2 a
x a
_) -> a
x) f (V2 a)
f) (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 :: forall a. Num a => V2 a -> V2 a
perp (V2 a
a a
b) = forall a. a -> a -> V2 a
V2 (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 = forall a. Epsilon a => a -> Bool
nearZero forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 forall a. Num a => a -> a -> a
* forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined::a)
  {-# INLINE sizeOf #-}
  alignment :: V2 a -> Int
alignment V2 a
_ = forall a. Storable a => a -> Int
alignment (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) = forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr a
ptr' a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
ptr' Int
1 a
y
    where ptr' :: Ptr a
ptr' = 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 = forall a. a -> a -> V2 a
V2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => Ptr a -> IO a
peek Ptr a
ptr' forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
ptr' Int
1
    where ptr' :: Ptr a
ptr' = 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) =
    [ forall a. a -> a -> V2 a
V2 a
i1 a
i2 | a
i1 <- forall a. Ix a => (a, a) -> [a]
range (a
l1,a
u1), a
i2 <- 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) =
    forall a. Ix a => (a, a) -> a -> Int
unsafeIndex (a
l1,a
u1) a
i1 forall a. Num a => a -> a -> a
* forall a. Ix a => (a, a) -> Int
unsafeRangeSize (a
l2,a
u2) forall a. Num a => a -> a -> a
+ 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) =
    forall a. Ix a => (a, a) -> a -> Bool
inRange (a
l1,a
u1) a
i1 Bool -> Bool -> 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 :: forall a. (Rep V2 -> a) -> V2 a
tabulate Rep V2 -> a
f = forall a. a -> a -> V2 a
V2 (Rep V2 -> a
f forall (t :: * -> *). R1 t => E t
ex) (Rep V2 -> a
f forall (t :: * -> *). R2 t => E t
ey)
  {-# INLINE tabulate #-}
  index :: forall a. V2 a -> Rep V2 -> a
index V2 a
xs (E forall a. Lens' (V2 a) a
l) = forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall a. Lens' (V2 a) a
l V2 a
xs
  {-# INLINE index #-}

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

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

instance WithIndex.TraversableWithIndex (E V2) V2 where
  itraverse :: forall (f :: * -> *) a b.
Applicative f =>
(E V2 -> a -> f b) -> V2 a -> f (V2 b)
itraverse E V2 -> a -> f b
f (V2 a
a a
b) = forall a. a -> a -> V2 a
V2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> E V2 -> a -> f b
f forall (t :: * -> *). R1 t => E t
ex a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> E V2 -> a -> f b
f 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 = forall (t :: * -> *). E t -> forall x. Lens' (t x) x
el Index (V2 a)
i
  {-# INLINE ix #-}

instance Each (V2 a) (V2 b) a b where
  each :: Traversal (V2 a) (V2 b) a b
each = 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 :: forall s. MVector s (V2 a) -> Int
basicLength (MV_V2 Int
n MVector s a
_) = Int
n
  basicUnsafeSlice :: forall s. Int -> Int -> MVector s (V2 a) -> MVector s (V2 a)
basicUnsafeSlice Int
m Int
n (MV_V2 Int
_ MVector s a
v) = forall s a. Int -> MVector s a -> MVector s (V2 a)
MV_V2 Int
n (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
M.basicUnsafeSlice (Int
2forall a. Num a => a -> a -> a
*Int
m) (Int
2forall a. Num a => a -> a -> a
*Int
n) MVector s a
v)
  basicOverlaps :: forall s. MVector s (V2 a) -> MVector s (V2 a) -> Bool
basicOverlaps (MV_V2 Int
_ MVector s a
v) (MV_V2 Int
_ MVector s a
u) = 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 :: forall s. Int -> ST s (MVector s (V2 a))
basicUnsafeNew Int
n = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (forall s a. Int -> MVector s a -> MVector s (V2 a)
MV_V2 Int
n) (forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
M.basicUnsafeNew (Int
2forall a. Num a => a -> a -> a
*Int
n))
  basicUnsafeRead :: forall s. MVector s (V2 a) -> Int -> ST s (V2 a)
basicUnsafeRead (MV_V2 Int
_ MVector s a
v) Int
i =
    do let o :: Int
o = Int
2forall a. Num a => a -> a -> a
*Int
i
       a
x <- forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
M.basicUnsafeRead MVector s a
v Int
o
       a
y <- forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
M.basicUnsafeRead MVector s a
v (Int
oforall a. Num a => a -> a -> a
+Int
1)
       forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> a -> V2 a
V2 a
x a
y)
  basicUnsafeWrite :: forall s. MVector s (V2 a) -> Int -> V2 a -> ST s ()
basicUnsafeWrite (MV_V2 Int
_ MVector s a
v) Int
i (V2 a
x a
y) =
    do let o :: Int
o = Int
2forall a. Num a => a -> a -> a
*Int
i
       forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
M.basicUnsafeWrite MVector s a
v Int
o     a
x
       forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
M.basicUnsafeWrite MVector s a
v (Int
oforall a. Num a => a -> a -> a
+Int
1) a
y
  basicInitialize :: forall s. MVector s (V2 a) -> ST s ()
basicInitialize (MV_V2 Int
_ MVector s a
v) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
M.basicInitialize MVector s 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 :: forall s. Mutable Vector s (V2 a) -> ST s (Vector (V2 a))
basicUnsafeFreeze (MV_V2 Int
n MVector s a
v) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ( forall a. Int -> Vector a -> Vector (V2 a)
V_V2 Int
n) (forall (v :: * -> *) a s. Vector v a => Mutable v s a -> ST s (v a)
G.basicUnsafeFreeze MVector s a
v)
  basicUnsafeThaw :: forall s. Vector (V2 a) -> ST s (Mutable Vector s (V2 a))
basicUnsafeThaw   ( V_V2 Int
n Vector a
v) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (forall s a. Int -> MVector s a -> MVector s (V2 a)
MV_V2 Int
n) (forall (v :: * -> *) a s. Vector v a => v a -> ST s (Mutable v s a)
G.basicUnsafeThaw   Vector a
v)
  basicLength :: Vector (V2 a) -> Int
basicLength       ( V_V2 Int
n Vector a
_) = Int
n
  basicUnsafeSlice :: Int -> Int -> Vector (V2 a) -> Vector (V2 a)
basicUnsafeSlice Int
m Int
n (V_V2 Int
_ Vector a
v) = forall a. Int -> Vector a -> Vector (V2 a)
V_V2 Int
n (forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
G.basicUnsafeSlice (Int
2forall a. Num a => a -> a -> a
*Int
m) (Int
2forall a. Num a => a -> a -> a
*Int
n) Vector a
v)
  basicUnsafeIndexM :: Vector (V2 a) -> Int -> Box (V2 a)
basicUnsafeIndexM (V_V2 Int
_ Vector a
v) Int
i =
    do let o :: Int
o = Int
2forall a. Num a => a -> a -> a
*Int
i
       a
x <- forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
G.basicUnsafeIndexM Vector a
v Int
o
       a
y <- forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
G.basicUnsafeIndexM Vector a
v (Int
oforall a. Num a => a -> a -> a
+Int
1)
       forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> a -> V2 a
V2 a
x a
y)

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

instance MonadFix V2 where
  mfix :: forall a. (a -> V2 a) -> V2 a
mfix a -> V2 a
f = 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 :: forall a. Floating a => a -> V2 a
angle a
a = forall a. a -> a -> V2 a
V2 (forall a. Floating a => a -> a
cos a
a) (forall a. Floating a => a -> a
sin a
a)

unangle :: (Floating a, Ord a) => V2 a -> a
unangle :: forall a. (Floating a, Ord a) => V2 a -> a
unangle a :: V2 a
a@(V2 a
ax a
ay) =
  let alpha :: a
alpha = forall a. Floating a => a -> a
asin forall a b. (a -> b) -> a -> b
$ a
ay forall a. Fractional a => a -> a -> a
/ forall (f :: * -> *) a. (Metric f, Floating a) => f a -> a
norm V2 a
a
  in if a
ax forall a. Ord a => a -> a -> Bool
< a
0
       then forall a. Floating a => a
pi 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 :: forall a. Num a => V2 a -> V2 a -> a
crossZ (V2 a
x1 a
y1) (V2 a
x2 a
y2) = a
x1forall a. Num a => a -> a -> a
*a
y2 forall a. Num a => a -> a -> a
- a
y1forall a. Num a => a -> a -> a
*a
x2
{-# INLINE crossZ #-}

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

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

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

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

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

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

instance Eq1 V2 where
  liftEq :: forall a b. (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 :: forall a b. (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 forall a. Monoid a => a -> a -> a
`mappend` a -> b -> Ordering
f a
b b
d
instance Read1 V2 where
  liftReadsPrec :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (V2 a)
liftReadsPrec Int -> ReadS a
f ReadS [a]
_ = forall a. (String -> ReadS a) -> Int -> ReadS a
readsData forall a b. (a -> b) -> a -> b
$ 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" forall a. a -> a -> V2 a
V2
instance Show1 V2 where
  liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> V2 a -> ShowS
liftShowsPrec Int -> a -> ShowS
f [a] -> ShowS
_ Int
d (V2 a
a a
b) = 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 :: Lens (V2 a) (V2 a) a a
_1 a -> f a
f (V2 a
x a
y) = a -> f a
f a
x forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
x' -> forall a. a -> a -> V2 a
V2 a
x' a
y

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

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

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