on cd4b4e2 help sign in
elk/libraries/Transform2D.elm
90 lines 2.4 kB history raw
1module Transform2D where
2{-| A library for performing [2D matrix transformations](http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations).
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. Since the third row as always 0 0
81, we omit this below.
10# Basic Transforms
11@docs identity, matrix, rotation
13# Multiplication
14@docs multiply
15-}
17import Native.Transform2D
19data Transform2D = Transform2D
21{-| Create an identity transform. Transforming by the identity does
22not change anything, but it can come in handy as a default or
23base case.
25 / 1 0 0 \
26 \ 0 1 0 /
27-}
28identity : Transform2D
29identity = Native.Transform2D.identity
31{-| Creates a transformation matrix. This lets you create transforms
32such as scales, shears, reflections, and translations.
34 matrix a b c d x y
36 / a b x \
37 \ c d y /
39Note that `x` and `y` are the translation values.
40-}
41matrix : Float -> Float -> Float -> Float -> Float -> Float -> Transform2D
42matrix = Native.Transform2D.matrix
44{-| Creates a [rotation matrix](http://en.wikipedia.org/wiki/Rotation_matrix).
45Given an angle t, it creates a counterclockwise rotation matrix:
47 / cos t -sin t 0 \
48 \ sin t cos t 0 /
49-}
50rotation : Float -> Transform2D
51rotation = Native.Transform2D.rotation
53{-| Creates a transformation matrix for translation:
55 translation x y
57 / 1 0 x \
58 \ 0 1 y /
59-}
60translation : Float -> Float -> Transform2D
61translation x y = matrix 1 0 0 1 x y
63{-| Creates a transformation matrix for scaling by a all directions:
65 scale s
67 / s 0 0 \
68 \ 0 s 0 /
69-}
70scale : Float -> Transform2D
71scale s = matrix s 0 0 s 0 0
73{-| Creates a transformation for horizontal scaling -}
74scaleX : Float -> Transform2D
75scaleX x = matrix x 0 0 1 0 0
77{-| Creates a transformation for vertical scaling -}
78scaleY : Float -> Transform2D
79scaleY y = matrix 1 0 0 y 0 0
81{-| Multiplies two transforms together:
83 multiply m n
85 / ma mb mx \ / na nb nx \
86 | mc md my | . | nc nd ny |
87 \ 0 0 1 / \ 0 0 1 /
88-}
89multiply : Transform2D -> Transform2D -> Transform2D
90multiply = Native.Transform2D.multiply