From 548e40add0fd3ebea18510f7c16a464680aed63c Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Jan 2025 16:25:14 -0800 Subject: Add vector scale. --- Spear/Math/Vector/Vector.hs | 3 +++ Spear/Math/Vector/Vector2.hs | 8 +++++++- Spear/Math/Vector/Vector3.hs | 3 +++ Spear/Math/Vector/Vector4.hs | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Spear/Math/Vector/Vector.hs b/Spear/Math/Vector/Vector.hs index e7f6d53..d9cd64f 100644 --- a/Spear/Math/Vector/Vector.hs +++ b/Spear/Math/Vector/Vector.hs @@ -48,3 +48,6 @@ class -- | Normalise the given vector. normalise :: v -> v + + -- | Scale the vector. + scale :: Float -> v -> v diff --git a/Spear/Math/Vector/Vector2.hs b/Spear/Math/Vector/Vector2.hs index 1ede3a9..b74cfef 100644 --- a/Spear/Math/Vector/Vector2.hs +++ b/Spear/Math/Vector/Vector2.hs @@ -33,7 +33,10 @@ type Position2 = Vector2 -- | Represents a vector in 2D. -data Vector2 = Vector2 {-# UNPACK #-} !Float {-# UNPACK #-} !Float deriving (Eq, Show) +data Vector2 = Vector2 + {-# UNPACK #-} !Float + {-# UNPACK #-} !Float + deriving (Eq, Show) instance Addition Vector2 Vector2 where @@ -129,6 +132,9 @@ instance Vector Vector2 where n = if n' == 0 then 1 else n' in ((1.0::Float) / n) * v + {-# INLINABLE scale #-} + scale s (Vector2 x y) = Vector2 (s*x) (s*y) + sizeFloat = sizeOf (undefined :: CFloat) diff --git a/Spear/Math/Vector/Vector3.hs b/Spear/Math/Vector/Vector3.hs index db5dc45..6ad4fe1 100644 --- a/Spear/Math/Vector/Vector3.hs +++ b/Spear/Math/Vector/Vector3.hs @@ -163,6 +163,9 @@ instance Vector Vector3 where n = if n' == 0 then 1 else n' in ((1.0::Float) / n) * v + {-# INLINABLE scale #-} + scale s (Vector3 x y z) = Vector3 (s*x) (s*y) (s*z) + sizeFloat = sizeOf (undefined :: CFloat) diff --git a/Spear/Math/Vector/Vector4.hs b/Spear/Math/Vector/Vector4.hs index 907295e..3ca27c9 100644 --- a/Spear/Math/Vector/Vector4.hs +++ b/Spear/Math/Vector/Vector4.hs @@ -161,6 +161,9 @@ instance Vector Vector4 where n = if n' == 0 then 1 else n' in ((1.0::Float) / n) * v + {-# INLINABLE scale #-} + scale s (Vector4 x y z w) = Vector4 (s*x) (s*y) (s*z) (s*w) + sizeFloat = sizeOf (undefined :: CFloat) -- cgit v1.2.3