@@ -88,7 +88,7 @@ defmodule Blendend.Matrix2D do
8888
8989 Examples:
9090
91- use Blended .Draw
91+ use Blendend .Draw
9292 m = matrix do
9393 rotate :math.pi
9494 end
@@ -120,7 +120,17 @@ defmodule Blendend.Matrix2D do
120120 # ===========================================================================
121121
122122 @ doc """
123- Pre-multiplies the matrix by `other` (`other * matrix`) and returns a new matrix.
123+ Returns `matrix * other`, i.e. the `other` transform is applied after `matrix`.
124+
125+ Examples:
126+
127+ iex> use Blendend.Draw
128+ iex> translate = matrix(do: translate(10, 0))
129+ iex> scale = matrix(do: scale(2, 1))
130+ iex> {:ok, m} = Blendend.Matrix2D.transform(translate, scale)
131+ iex> {:ok, list} = Blendend.Matrix2D.to_list(m)
132+ iex> list
133+ [2.0, 0.0, 0.0, 1.0, 10.0, 0.0] # translate first, then scale (translation unchanged)
124134 """
125135 @ spec transform ( t ( ) , t ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
126136 def transform ( m , other ) , do: Native . matrix2d_transform ( m , other )
@@ -137,7 +147,17 @@ defmodule Blendend.Matrix2D do
137147 end
138148
139149 @ doc """
140- Post-multiplies the matrix by `other` (`matrix * other`) and returns a new matrix.
150+ Returns `other * matrix`, i.e. the `other` transform is applied before `matrix`.
151+
152+ Examples:
153+
154+ iex> use Blendend.Draw
155+ iex> translate = matrix(do: translate(10, 0))
156+ iex> scale = matrix(do: scale(2, 1))
157+ iex> {:ok, m} = Blendend.Matrix2D.post_transform(translate, scale)
158+ iex> {:ok, list} = Blendend.Matrix2D.to_list(m)
159+ iex> list
160+ [2.0, 0.0, 0.0, 1.0, 20.0, 0.0] # scale first, then translate (x translation doubled)
141161 """
142162 @ spec post_transform ( t ( ) , t ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
143163 def post_transform ( m , other ) , do: Native . matrix2d_post_transform ( m , other )
@@ -229,7 +249,7 @@ defmodule Blendend.Matrix2D do
229249 end
230250
231251 @ doc """
232- Post-multiplies the matrix by a scale and returns a new matrix .
252+ Returns `scale * matrix`, applying the scale before the existing transform .
233253 """
234254 @ spec post_scale ( t ( ) , number ( ) , number ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
235255 def post_scale ( m , sx , sy ) , do: Native . matrix2d_post_scale ( m , sx * 1.0 , sy * 1.0 )
@@ -289,7 +309,7 @@ defmodule Blendend.Matrix2D do
289309 end
290310
291311 @ doc """
292- Post-multiplies the matrix by a rotation about `{cx, cy}`.
312+ Returns `rotation * matrix`, applying the rotation about `{cx, cy}` before the existing transform .
293313 """
294314 @ spec post_rotate ( t ( ) , number ( ) , number ( ) , number ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
295315 def post_rotate ( m , angle_rad , cx , cy ) ,
@@ -307,7 +327,7 @@ defmodule Blendend.Matrix2D do
307327 end
308328
309329 @ doc """
310- Post-translate the matrix by `(tx, ty)` (applied after existing transforms) .
330+ Returns `translation * matrix`, applying `(tx, ty)` before the existing transforms.
311331 """
312332 @ spec post_translate ( t ( ) , number ( ) , number ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
313333 def post_translate ( m , tx , ty ) , do: Native . matrix2d_post_translate ( m , tx * 1.0 , ty * 1.0 )
@@ -323,8 +343,32 @@ defmodule Blendend.Matrix2D do
323343 end
324344 end
325345
346+ @ doc """
347+ Returns `skew * matrix`, applying the shear before the existing transforms.
348+ """
349+ @ spec post_skew ( t ( ) , number ( ) , number ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
350+ def post_skew ( m , kx , ky ) , do: Native . matrix2d_post_skew ( m , kx * 1.0 , ky * 1.0 )
351+
352+ @ doc """
353+ Same as `post_skew/3`, but returns the skewed matrix directly.
354+ """
355+ @ spec post_skew! ( t ( ) , number ( ) , number ( ) ) :: t ( )
356+ def post_skew! ( m , kx , ky ) do
357+ case post_skew ( m , kx , ky ) do
358+ { :ok , m2 } -> m2
359+ { :error , reason } -> raise Error . new ( :matrix2d_post_skew , reason )
360+ end
361+ end
362+
326363 @ doc """
327364 Inverts the matrix, returning a new matrix.
365+
366+ Examples:
367+
368+ iex> use Blendend.Draw
369+ iex> scale = matrix(do: scale(2, 1))
370+ iex> Blendend.Matrix2D.invert!(scale) |> Blendend.Matrix2D.to_list!
371+ [0.5, -0.0, -0.0, 1.0, -0.0, -0.0]
328372 """
329373 @ spec invert ( t ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
330374 def invert ( m ) , do: Native . matrix2d_invert ( m )
@@ -359,6 +403,17 @@ defmodule Blendend.Matrix2D do
359403
360404 @ doc """
361405 Maps a vector `{x, y}` through the matrix (ignores translation), returning `{ok, {x, y}}`.
406+
407+ Examples:
408+
409+ iex> use Blendend.Draw
410+ iex> scale = matrix(do: scale(2, 1))
411+ iex> Blendend.Matrix2D.map_vector!(scale, 2, 3)
412+ {4.0, 3.0}
413+
414+ iex> translate = matrix(do: translate(10, 10))
415+ iex> Blendend.Matrix2D.map_vector!(translate, 2, 3)
416+ {2.0, 3.0}
362417 """
363418 @ spec map_vector ( t ( ) , number ( ) , number ( ) ) :: { :ok , { number ( ) , number ( ) } } | { :error , term ( ) }
364419 def map_vector ( m , x , y ) , do: Native . matrix2d_map_vector ( m , x * 1.0 , y * 1.0 )
@@ -391,21 +446,4 @@ defmodule Blendend.Matrix2D do
391446 { :error , reason } -> raise Error . new ( :matrix2d_make_sin_cos , reason )
392447 end
393448 end
394-
395- @ doc """
396- Post-multiplies the matrix by a skew (shear).
397- """
398- @ spec post_skew ( t ( ) , number ( ) , number ( ) ) :: { :ok , t ( ) } | { :error , term ( ) }
399- def post_skew ( m , kx , ky ) , do: Native . matrix2d_post_skew ( m , kx * 1.0 , ky * 1.0 )
400-
401- @ doc """
402- Same as `post_skew/3`, but returns the skewed matrix directly.
403- """
404- @ spec post_skew! ( t ( ) , number ( ) , number ( ) ) :: t ( )
405- def post_skew! ( m , kx , ky ) do
406- case post_skew ( m , kx , ky ) do
407- { :ok , m2 } -> m2
408- { :error , reason } -> raise Error . new ( :matrix2d_post_skew , reason )
409- end
410- end
411449end
0 commit comments