on 577fc17 help sign in
elk/libraries/Transform2D.elm
96 lines 2.7 kB history raw
1module Transform2D ( Transform2D
2 , identity, matrix, multiply
3 , rotation, translation
4 , scale, scaleX, scaleY
5 ) where
6{-| A library for performing [2D matrix transformations][affine].
7It is used primarily with the `groupTransform` function from `Graphics.Collage` and
8allows you to do things like rotation, scaling, translation, shearing, and reflection.
10Note that all the matrices in this library are 3x3 matrices of homogeneous
11coordinates, used for [affine transformations][affine]. Since the bottom row as
12always `0 0 1` in these matrices, it is omitted in the diagrams below.
14 [affine]: http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations
16# Transforms
17@docs identity, matrix, rotation, translation, scale, scaleX, scaleY
19# Multiplication
20@docs multiply
21-}
23import Native.Transform2D
25data Transform2D = Transform2D
27{-| Create an identity transform. Transforming by the identity does
28not change anything, but it can come in handy as a default or
29base case.
31 / 1 0 0 \
32 \ 0 1 0 /
33-}
34identity : Transform2D
35identity = Native.Transform2D.identity
37{-| Creates a transformation matrix. This lets you create transforms
38such as scales, shears, reflections, and translations.
40 matrix a b c d x y
42 / a b x \
43 \ c d y /
45Note that `x` and `y` are the translation values.
46-}
47matrix : Float -> Float -> Float -> Float -> Float -> Float -> Transform2D
48matrix = Native.Transform2D.matrix
50{-| Creates a [rotation matrix](http://en.wikipedia.org/wiki/Rotation_matrix).
51Given an angle t, it creates a counterclockwise rotation matrix:
53 / cos t -sin t 0 \
54 \ sin t cos t 0 /
55-}
56rotation : Float -> Transform2D
57rotation = Native.Transform2D.rotation
59{-| Creates a transformation matrix for translation:
61 translation x y
63 / 1 0 x \
64 \ 0 1 y /
65-}
66translation : Float -> Float -> Transform2D
67translation x y = matrix 1 0 0 1 x y
69{-| Creates a transformation matrix for scaling by a all directions:
71 scale s
73 / s 0 0 \
74 \ 0 s 0 /
75-}
76scale : Float -> Transform2D
77scale s = matrix s 0 0 s 0 0
79{-| Creates a transformation for horizontal scaling -}
80scaleX : Float -> Transform2D
81scaleX x = matrix x 0 0 1 0 0
83{-| Creates a transformation for vertical scaling -}
84scaleY : Float -> Transform2D
85scaleY y = matrix 1 0 0 y 0 0
87{-| Multiplies two transforms together:
89 multiply m n
91 / ma mb mx \ / na nb nx \
92 | mc md my | . | nc nd ny |
93 \ 0 0 1 / \ 0 0 1 /
94-}
95multiply : Transform2D -> Transform2D -> Transform2D
96multiply = Native.Transform2D.multiply