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
|
{-# LANGUAGE TypeOperators, TypeSynonymInstances #-}
module Transformable ( Coord
, Vector3
, Transform
, Transformable(..)
, translate
, rotate
, scale
) where
import Data.LinearMap
type Coord = Double
type Vector3 = (Coord, Coord, Coord)
type Transform = Vector3 :-* Vector3
class Transformable a where
(><) :: Transform -> a -> a
instance Transformable Transform where
t1 >< t2 = t1 *.* t2
instance Transformable Vector3 where
t >< v = t `lapply` v
translate :: Coord -> Coord -> Transform
translate dx dy = linear $ \(x, y, w) -> (x + w*dx, y + w*dy, w)
rotate :: Coord -> Transform
rotate a = linear $ \(x, y, w) -> (c*x - s*y, s*x + c*y, w) where
c = cos a
s = sin a
scale :: Coord -> Transform
scale s = linear $ \(x, y, w) -> (s*y, s*y, w)
|