{- |
  2-dimensional vectors with vector arithmetic.
-}

module Data.Vector.V2 where

import Data.Vector.Class

data Vector2 = Vector2 {Vector2 -> Scalar
v2x, Vector2 -> Scalar
v2y :: {-# UNPACK #-} !Scalar} deriving (Vector2 -> Vector2 -> Bool
(Vector2 -> Vector2 -> Bool)
-> (Vector2 -> Vector2 -> Bool) -> Eq Vector2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Vector2 -> Vector2 -> Bool
== :: Vector2 -> Vector2 -> Bool
$c/= :: Vector2 -> Vector2 -> Bool
/= :: Vector2 -> Vector2 -> Bool
Eq, Int -> Vector2 -> ShowS
[Vector2] -> ShowS
Vector2 -> String
(Int -> Vector2 -> ShowS)
-> (Vector2 -> String) -> ([Vector2] -> ShowS) -> Show Vector2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Vector2 -> ShowS
showsPrec :: Int -> Vector2 -> ShowS
$cshow :: Vector2 -> String
show :: Vector2 -> String
$cshowList :: [Vector2] -> ShowS
showList :: [Vector2] -> ShowS
Show)

instance BasicVector Vector2 where
  vmap :: (Scalar -> Scalar) -> Vector2 -> Vector2
vmap  Scalar -> Scalar
f (Vector2 Scalar
x  Scalar
y )                 = Scalar -> Scalar -> Vector2
Vector2 (Scalar -> Scalar
f Scalar
x)     (Scalar -> Scalar
f Scalar
y)
  vzip :: (Scalar -> Scalar -> Scalar) -> Vector2 -> Vector2 -> Vector2
vzip  Scalar -> Scalar -> Scalar
f (Vector2 Scalar
x1 Scalar
y1) (Vector2 Scalar
x2 Scalar
y2) = Scalar -> Scalar -> Vector2
Vector2 (Scalar -> Scalar -> Scalar
f Scalar
x1 Scalar
x2) (Scalar -> Scalar -> Scalar
f Scalar
y1 Scalar
y2)
  vfold :: (Scalar -> Scalar -> Scalar) -> Vector2 -> Scalar
vfold Scalar -> Scalar -> Scalar
f (Vector2 Scalar
x  Scalar
y )                 = Scalar -> Scalar -> Scalar
f Scalar
x Scalar
y

  vpack :: [Scalar] -> Maybe Vector2
vpack (Scalar
x:Scalar
y:[Scalar]
_) = Vector2 -> Maybe Vector2
forall a. a -> Maybe a
Just (Vector2 -> Maybe Vector2) -> Vector2 -> Maybe Vector2
forall a b. (a -> b) -> a -> b
$ Scalar -> Scalar -> Vector2
Vector2 Scalar
x Scalar
y
  vpack [Scalar]
_       = Maybe Vector2
forall a. Maybe a
Nothing

  vunpack :: Vector2 -> [Scalar]
vunpack (Vector2 Scalar
x Scalar
y) = [Scalar
x,Scalar
y]

  vpromote :: Scalar -> Vector2
vpromote Scalar
x = Scalar -> Scalar -> Vector2
Vector2 Scalar
x Scalar
x

instance Num Vector2 where
  + :: Vector2 -> Vector2 -> Vector2
(+) = (Scalar -> Scalar -> Scalar) -> Vector2 -> Vector2 -> Vector2
forall v.
BasicVector v =>
(Scalar -> Scalar -> Scalar) -> v -> v -> v
vzip Scalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
(+)
  (-) = (Scalar -> Scalar -> Scalar) -> Vector2 -> Vector2 -> Vector2
forall v.
BasicVector v =>
(Scalar -> Scalar -> Scalar) -> v -> v -> v
vzip (-)
  * :: Vector2 -> Vector2 -> Vector2
(*) = (Scalar -> Scalar -> Scalar) -> Vector2 -> Vector2 -> Vector2
forall v.
BasicVector v =>
(Scalar -> Scalar -> Scalar) -> v -> v -> v
vzip Scalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
(*)
  abs :: Vector2 -> Vector2
abs = (Scalar -> Scalar) -> Vector2 -> Vector2
forall v. BasicVector v => (Scalar -> Scalar) -> v -> v
vmap Scalar -> Scalar
forall a. Num a => a -> a
abs
  signum :: Vector2 -> Vector2
signum = (Scalar -> Scalar) -> Vector2 -> Vector2
forall v. BasicVector v => (Scalar -> Scalar) -> v -> v
vmap Scalar -> Scalar
forall a. Num a => a -> a
signum
  fromInteger :: Integer -> Vector2
fromInteger = Scalar -> Vector2
forall v. BasicVector v => Scalar -> v
vpromote (Scalar -> Vector2) -> (Integer -> Scalar) -> Integer -> Vector2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Scalar
forall a. Num a => Integer -> a
fromInteger

instance Fractional Vector2 where
  / :: Vector2 -> Vector2 -> Vector2
(/) = (Scalar -> Scalar -> Scalar) -> Vector2 -> Vector2 -> Vector2
forall v.
BasicVector v =>
(Scalar -> Scalar -> Scalar) -> v -> v -> v
vzip Scalar -> Scalar -> Scalar
forall a. Fractional a => a -> a -> a
(/)
  recip :: Vector2 -> Vector2
recip = (Scalar -> Scalar) -> Vector2 -> Vector2
forall v. BasicVector v => (Scalar -> Scalar) -> v -> v
vmap Scalar -> Scalar
forall a. Fractional a => a -> a
recip
  fromRational :: Rational -> Vector2
fromRational = Scalar -> Vector2
forall v. BasicVector v => Scalar -> v
vpromote (Scalar -> Vector2) -> (Rational -> Scalar) -> Rational -> Vector2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Scalar
forall a. Fractional a => Rational -> a
fromRational

instance Vector Vector2 where