File: Arithmetic.hs

package info (click to toggle)
haskell-gloss 1.13.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 344 kB
  • sloc: haskell: 2,903; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,163 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE BangPatterns #-}

-- |
-- == Point and vector arithmetic
--
-- Vectors aren't numbers according to Haskell, because they don't
-- support all numeric operations sensibly. We define component-wise
-- addition, subtraction, and negation along with scalar multiplication
-- in this module, which is intended to be imported qualified.
module Graphics.Gloss.Data.Point.Arithmetic
  (
    Point
  , (+)
  , (-)
  , (*)
  , negate
  ) where
import Prelude (Float)
import qualified Prelude as P
import Graphics.Gloss.Rendering (Point)

infixl 6 +, -
infixl 7 *

-- | Add two vectors, or add a vector to a point.
(+) :: Point -> Point -> Point
(x1, y1) + (x2, y2) =
  let
    !x = x1 P.+ x2
    !y = y1 P.+ y2
  in (x, y)

-- | Subtract two vectors, or subtract a vector from a point.
(-) :: Point -> Point -> Point
(x1, y1) - (x2, y2) =
  let
    !x = x1 P.- x2
    !y = y1 P.- y2
  in (x, y)

-- | Negate a vector.
negate :: Point -> Point
negate (x, y) =
  let
    !x' = P.negate x
    !y' = P.negate y
  in (x', y')

-- | Multiply a scalar by a vector.
(*) :: Float -> Point -> Point
(*) s (x, y) =
  let
    !x' = s P.* x
    !y' = s P.* y
  in (x', y')