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