packages feed

HGamer3D-Data-0.1.5: HGamer3D/Data/Vector2.hs

-- This source file is part of HGamer3D
-- (A project to enable 3D game development in Haskell)
-- For the latest info, see http://www.althainz.de/HGamer3D.html
--
-- (c) 2011 Peter Althainz
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- Vector2.hs

module HGamer3D.Data.Vector2 

(
	-- export Vector2 Type and its functions again
	
	Vector2 (Vector2),
	vector2,
	uniV2,
	v2X,v2Y,
	HGamer3D.Data.Vector2.dotV2,
	HGamer3D.Data.Vector2.normSqV2,
	HGamer3D.Data.Vector2.normV2,
	HGamer3D.Data.Vector2.normalizeV2,
	HGamer3D.Data.Vector2.scaleV2
	
)

where

import Data.Vec.Base
import Data.Vec.Nat
import Data.Vec.Packed
import Data.Vec.LinAlg

newtype Vector2 = Vector2 Vec2F

vector2 :: Float -> Float -> Vector2
vector2 a b = Vector2 ( pack (a :. b :. ()) )

uniV2 :: Float -> Vector2
uniV2 f = Vector2 (pack (vec f))

v2X :: Vector2 -> Float
v2X (Vector2 v2) = get n0 v2

v2Y :: Vector2 -> Float
v2Y (Vector2 v2) = get n1 v2



liftv :: (a -> Vec2F) -> (a -> Vector2)
liftv f = f1 where
	f1 a1 = (Vector2 (f a1)) 


lift1 :: (Vec2F -> a) -> (Vector2 -> a)
lift1 f = f1 where
	f1 (Vector2 a1) = f a1 

lift1v :: (Vec2F -> Vec2F) -> (Vector2 -> Vector2)
lift1v f = f2 where
	f2 a = (Vector2 (f1 a))
	f1 (Vector2 a1) = f a1 

lift2 :: (Vec2F -> Vec2F -> a) -> (Vector2 -> Vector2 -> a)
lift2 f = f1 where
	f1 (Vector2 a1) (Vector2 b1) = f a1 b1 

lift2v :: (Vec2F -> Vec2F -> Vec2F) -> (Vector2 -> Vector2 -> Vector2)
lift2v f = f2 where
	f2 a b = (Vector2 (f1 a b))
	f1 (Vector2 a1) (Vector2 b1) = f a1 b1 

lift2vp :: (Vec2 Float -> Vec2 Float -> Vec2 Float) -> (Vector2 -> Vector2 -> Vector2)
lift2vp f = f2 where
	f2 a b = (Vector2 (pack (f1 a b)))
	f1 (Vector2 (Vec2F a1 a2)) (Vector2 (Vec2F b1 b2)) = f (a1 :. a2 :.  ()) (b1 :. b2 :. ()) 

instance Show Vector2 where
	show = lift1 (Prelude.show)
	
instance Eq Vector2 where
	(==) = lift2 (Prelude.==)
	 
instance Num Vector2 where
	abs = lift1v (Prelude.abs)
	signum = lift1v (Prelude.signum)
	fromInteger = liftv (Prelude.fromInteger)

	(+) = lift2v (Prelude.+)
	(-) = lift2v (Prelude.-)
	(*) = lift2v (Prelude.*)

dotV2 :: Vector2 -> Vector2 -> Float
dotV2 = lift2 Data.Vec.LinAlg.dot

normSqV2 :: Vector2 -> Float
normSqV2 = lift1 Data.Vec.LinAlg.normSq

normV2 :: Vector2 -> Float
normV2 = lift1 Data.Vec.LinAlg.norm

normalizeV2 :: Vector2 -> Vector2
normalizeV2 = lift1v Data.Vec.LinAlg.normalize

scaleV2 :: Float -> Vector2 -> Vector2
scaleV2 f (Vector2 v) = Vector2 (Data.Vec.Base.map (*f) v)