{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE MultiParamTypeClasses     #-}
{-# LANGUAGE StandaloneDeriving        #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
-- |
-- Module      :  Data.Point2
-- Copyright   :  (c) Antony Courtney and Henrik Nilsson, Yale University, 2003
-- License     :  BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer  :  ivan.perez@keera.co.uk
-- Stability   :  provisional
-- Portability :  non-portable (GHC extensions)
--
-- 2D point abstraction (R^2).
module Data.Point2
    ( Point2(..) -- Non-abstract, instance of AffineSpace
    , point2X    -- :: RealFloat a => Point2 a -> a
    , point2Y    -- :: RealFloat a => Point2 a -> a
    )
  where

-- External imports
import Control.DeepSeq (NFData(..))

-- Internal imports
import Data.AffineSpace
import Data.Vector2
import Data.VectorSpace ()

-- * 2D point, constructors and selectors

-- | 2D point.
data Point2 a = RealFloat a => Point2 !a !a

deriving instance Eq a => Eq (Point2 a)

deriving instance Show a => Show (Point2 a)

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

-- | X coordinate of a 2D point.
point2X :: RealFloat a => Point2 a -> a
point2X :: forall a. RealFloat a => Point2 a -> a
point2X (Point2 a
x a
_) = a
x

-- | Y coordinate of a 2D point.
point2Y :: RealFloat a => Point2 a -> a
point2Y :: forall a. RealFloat a => Point2 a -> a
point2Y (Point2 a
_ a
y) = a
y

-- * Affine space instance

instance RealFloat a => AffineSpace (Point2 a) (Vector2 a) a where
  origin :: Point2 a
origin = forall a. RealFloat a => a -> a -> Point2 a
Point2 a
0 a
0

  (Point2 a
x a
y) .+^ :: Point2 a -> Vector2 a -> Point2 a
.+^ Vector2 a
v = forall a. RealFloat a => a -> a -> Point2 a
Point2 (a
x forall a. Num a => a -> a -> a
+ forall a. RealFloat a => Vector2 a -> a
vector2X Vector2 a
v) (a
y forall a. Num a => a -> a -> a
+ forall a. RealFloat a => Vector2 a -> a
vector2Y Vector2 a
v)

  (Point2 a
x a
y) .-^ :: Point2 a -> Vector2 a -> Point2 a
.-^ Vector2 a
v = forall a. RealFloat a => a -> a -> Point2 a
Point2 (a
x forall a. Num a => a -> a -> a
- forall a. RealFloat a => Vector2 a -> a
vector2X Vector2 a
v) (a
y forall a. Num a => a -> a -> a
- forall a. RealFloat a => Vector2 a -> a
vector2Y Vector2 a
v)

  (Point2 a
x1 a
y1) .-. :: Point2 a -> Point2 a -> Vector2 a
.-. (Point2 a
x2 a
y2) = forall a. RealFloat a => a -> a -> Vector2 a
vector2 (a
x1 forall a. Num a => a -> a -> a
- a
x2) (a
y1 forall a. Num a => a -> a -> a
- a
y2)