on master help sign in
Merge pull request #307 from Sheeo/transform
5edfdb0Evan Czaplicki
libraries/Native/Transform2D.js−1
@@ -37,7 +37,6 @@ Elm.Native.Transform2D.make = function(elm) {
37 var s = Math.sin(t);
37 var s = Math.sin(t);
38 return new A([c, -s, 0, s, c, 0]);
38 return new A([c, -s, 0, s, c, 0]);
39 }
39 }
40−
41 function rotate(t,m) {
40 function rotate(t,m) {
42 var c = Math.cos(t);
41 var c = Math.cos(t);
43 var s = Math.sin(t);
42 var s = Math.sin(t);
libraries/Transform2D.elm+41−8
@@ -3,6 +3,10 @@ module Transform2D where
3It is used primarily with the `groupTransform` function from `Graphics.Collage` and
3It is used primarily with the `groupTransform` function from `Graphics.Collage` and
4allows you to do things like rotation, scaling, translation, shearing, and reflection.
4allows you to do things like rotation, scaling, translation, shearing, and reflection.
5
5
6+Note that all the matrices in this library are 3x3 matrices of homogeneous
7+coordinates, used for affine transformations. Since the third row as always 0 0
8+1, we omit this below.
9+
6# Basic Transforms
10# Basic Transforms
7@docs identity, matrix, rotation
11@docs identity, matrix, rotation
8
12
@@ -18,8 +22,8 @@ data Transform2D = Transform2D
18not change anything, but it can come in handy as a default or
22not change anything, but it can come in handy as a default or
19base case.
23base case.
20
24
21− / 1 0 \
25+ / 1 0 0 \
22− \ 0 1 /
26+ \ 0 1 0 /
23-}
27-}
24identity : Transform2D
28identity : Transform2D
25identity = Native.Transform2D.identity
29identity = Native.Transform2D.identity
@@ -29,10 +33,10 @@ such as scales, shears, reflections, and translations.
29
33
30 matrix a b c d dx dy
34 matrix a b c d dx dy
31
35
32− / a b \
36+ / a b dx \
33− \ c d /
37+ \ c d dy /
34
38
35−And `dx` and `dy` are the translation values.
39+Note that `dx` and `dy` are the translation values.
36-}
40-}
37matrix : Float -> Float -> Float -> Float -> Float -> Float -> Transform2D
41matrix : Float -> Float -> Float -> Float -> Float -> Float -> Transform2D
38matrix = Native.Transform2D.matrix
42matrix = Native.Transform2D.matrix
@@ -40,12 +44,41 @@ matrix = Native.Transform2D.matrix
40{-| Creates a [rotation matrix](http://en.wikipedia.org/wiki/Rotation_matrix).
44{-| Creates a [rotation matrix](http://en.wikipedia.org/wiki/Rotation_matrix).
41Given an angle t, it creates a counterclockwise rotation matrix:
45Given an angle t, it creates a counterclockwise rotation matrix:
42
46
43− / cos t -sin t \
47+ / cos t -sin t 0 \
44− \ sin t cos t /
48+ \ sin t cos t 0 /
45-}
49-}
46rotation : Float -> Transform2D
50rotation : Float -> Transform2D
47rotation = Native.Transform2D.rotation
51rotation = Native.Transform2D.rotation
48
52
53+{-| Creates a transformation matrix for translation:
54+
55+ translation x y
56+
57+ / 1 0 x \
58+ | 0 1 y |
59+ \ 0 0 1 /
60+-}
61+translation : Float -> Float -> Transform2D
62+translation x y = matrix 1 0 0 1 x y
63+
64+{-| Creates a transformation matrix for scaling by a all directions:
65+
66+ scale s
67+
68+ / s 0 0 \
69+ \ 0 s 0 /
70+-}
71+scale : Float -> Transform2D
72+scale s = matrix s 0 0 s 0 0
73+
74+{-| Creates a transformation for horizontal scaling -}
75+scaleX : Float -> Transform2D
76+scaleX x = matrix x 0 0 1 0 0
77+
78+{-| Creates a transformation for vertical scaling -}
79+scaleY : Float -> Transform2D
80+scaleY y = matrix y 0 0 1 0 0
81+
49{-| Multiplies two transforms together:
82{-| Multiplies two transforms together:
50
83
51 multiply a b
84 multiply a b
@@ -54,4 +87,4 @@ rotation = Native.Transform2D.rotation
54 \ a21 a22 / \ b21 b22 /
87 \ a21 a22 / \ b21 b22 /
55-}
88-}
56multiply : Transform2D -> Transform2D -> Transform2D
89multiply : Transform2D -> Transform2D -> Transform2D
57−multiply = Native.Transform2D.multiply
90+multiply = Native.Transform2D.multiply