2013-08-19 18:57:18 +00:00
|
|
|
module Transform2D where
|
2013-09-12 19:11:35 +00:00
|
|
|
{-| A library for performing [2D matrix transformations](http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations).
|
|
|
|
It is used primarily with the `groupTransform` function from `Graphics.Collage` and
|
|
|
|
allows you to do things like rotation, scaling, translation, shearing, and reflection.
|
2013-09-12 19:34:44 +00:00
|
|
|
|
2013-10-29 16:05:17 +00:00
|
|
|
Note that all the matrices in this library are 3x3 matrices of homogeneous
|
|
|
|
coordinates, used for affine transformations. Since the third row as always 0 0
|
|
|
|
1, we omit this below.
|
|
|
|
|
2013-09-12 19:34:44 +00:00
|
|
|
# Basic Transforms
|
|
|
|
@docs identity, matrix, rotation
|
|
|
|
|
|
|
|
# Multiplication
|
|
|
|
@docs multiply
|
2013-09-12 19:11:35 +00:00
|
|
|
-}
|
2013-03-24 06:54:00 +00:00
|
|
|
|
2013-08-19 18:57:18 +00:00
|
|
|
import Native.Transform2D
|
2013-03-24 06:54:00 +00:00
|
|
|
|
2013-08-19 18:57:18 +00:00
|
|
|
data Transform2D = Transform2D
|
2013-07-26 10:25:04 +00:00
|
|
|
|
2013-09-12 19:11:35 +00:00
|
|
|
{-| Create an identity transform. Transforming by the identity does
|
|
|
|
not change anything, but it can come in handy as a default or
|
|
|
|
base case.
|
|
|
|
|
2013-10-29 16:05:17 +00:00
|
|
|
/ 1 0 0 \
|
|
|
|
\ 0 1 0 /
|
2013-09-12 19:11:35 +00:00
|
|
|
-}
|
2013-08-19 18:57:18 +00:00
|
|
|
identity : Transform2D
|
|
|
|
identity = Native.Transform2D.identity
|
2013-03-24 06:54:00 +00:00
|
|
|
|
2013-09-12 19:11:35 +00:00
|
|
|
{-| Creates a transformation matrix. This lets you create transforms
|
|
|
|
such as scales, shears, reflections, and translations.
|
|
|
|
|
2013-10-31 11:23:42 +00:00
|
|
|
matrix a b c d x y
|
2013-09-12 19:11:35 +00:00
|
|
|
|
2013-10-31 11:23:42 +00:00
|
|
|
/ a b x \
|
|
|
|
\ c d y /
|
2013-09-12 19:11:35 +00:00
|
|
|
|
2013-10-31 11:23:42 +00:00
|
|
|
Note that `x` and `y` are the translation values.
|
2013-09-12 19:11:35 +00:00
|
|
|
-}
|
2013-08-19 18:57:18 +00:00
|
|
|
matrix : Float -> Float -> Float -> Float -> Float -> Float -> Transform2D
|
|
|
|
matrix = Native.Transform2D.matrix
|
2013-03-24 06:54:00 +00:00
|
|
|
|
2013-09-12 19:11:35 +00:00
|
|
|
{-| Creates a [rotation matrix](http://en.wikipedia.org/wiki/Rotation_matrix).
|
|
|
|
Given an angle t, it creates a counterclockwise rotation matrix:
|
|
|
|
|
2013-10-29 16:05:17 +00:00
|
|
|
/ cos t -sin t 0 \
|
|
|
|
\ sin t cos t 0 /
|
2013-09-12 19:11:35 +00:00
|
|
|
-}
|
2013-08-19 18:57:18 +00:00
|
|
|
rotation : Float -> Transform2D
|
|
|
|
rotation = Native.Transform2D.rotation
|
2013-05-17 20:46:08 +00:00
|
|
|
|
2013-10-21 12:53:58 +00:00
|
|
|
{-| Creates a transformation matrix for translation:
|
|
|
|
|
|
|
|
translation x y
|
|
|
|
|
|
|
|
/ 1 0 x \
|
2013-10-31 11:23:42 +00:00
|
|
|
\ 0 1 y /
|
2013-10-21 12:53:58 +00:00
|
|
|
-}
|
2013-10-21 11:59:54 +00:00
|
|
|
translation : Float -> Float -> Transform2D
|
2013-10-21 12:53:58 +00:00
|
|
|
translation x y = matrix 1 0 0 1 x y
|
|
|
|
|
|
|
|
{-| Creates a transformation matrix for scaling by a all directions:
|
|
|
|
|
2013-10-31 11:01:41 +00:00
|
|
|
scale s
|
2013-10-21 12:53:58 +00:00
|
|
|
|
2013-10-31 11:01:41 +00:00
|
|
|
/ s 0 0 \
|
|
|
|
\ 0 s 0 /
|
2013-10-21 12:53:58 +00:00
|
|
|
-}
|
|
|
|
scale : Float -> Transform2D
|
2013-10-31 11:01:41 +00:00
|
|
|
scale s = matrix s 0 0 s 0 0
|
2013-10-21 11:59:54 +00:00
|
|
|
|
2013-10-21 13:02:25 +00:00
|
|
|
{-| Creates a transformation for horizontal scaling -}
|
|
|
|
scaleX : Float -> Transform2D
|
|
|
|
scaleX x = matrix x 0 0 1 0 0
|
|
|
|
|
|
|
|
{-| Creates a transformation for vertical scaling -}
|
|
|
|
scaleY : Float -> Transform2D
|
2013-10-31 11:23:42 +00:00
|
|
|
scaleY y = matrix 1 0 0 y 0 0
|
2013-10-21 13:02:25 +00:00
|
|
|
|
2013-09-12 19:11:35 +00:00
|
|
|
{-| Multiplies two transforms together:
|
|
|
|
|
2013-10-31 11:23:42 +00:00
|
|
|
multiply m n
|
2013-09-12 19:11:35 +00:00
|
|
|
|
2013-10-31 11:23:42 +00:00
|
|
|
/ ma mb mx \ / na nb nx \
|
|
|
|
| mc md my | . | nc nd ny |
|
|
|
|
\ 0 0 1 / \ 0 0 1 /
|
2013-09-12 19:11:35 +00:00
|
|
|
-}
|
2013-08-19 18:57:18 +00:00
|
|
|
multiply : Transform2D -> Transform2D -> Transform2D
|
2013-10-21 11:59:54 +00:00
|
|
|
multiply = Native.Transform2D.multiply
|