Macaw.Draw.IR.Transform (macaw_draw v0.1.0)

Copy Markdown View Source

A node's transform (spec/10 §1.3), copied from visualize's Visualize.IR.Transform: %Macaw.Draw.IR.Transform{operations}, applied left to right as SVG composes them.

An operation is {:translate, x, y}, {:rotate, a}, {:rotate, a, cx, cy}, {:scale, sx, sy}, {:skew_x, a}, {:skew_y, a} or {:matrix, a, b, c, d, e, f}, angles in degrees. to_matrix/1 folds them into one affine matrix, M = Op₁ · … · Opₙ; to_string/1 writes the transform attribute, every number through Macaw.Draw.IR.Path.format_number/1, a uniform scale as scale(s).

iex> alias Macaw.Draw.IR.Transform
iex> Transform.new() |> Transform.translate(100, 50) |> Transform.rotate(45) |> Transform.scale(2) |> Transform.to_string()
"translate(100,50) rotate(45) scale(2)"

An operation of no shape above, in a transform built by hand, is the fault {[:operations, i], {:bad_transform, op}} of to_matrix/1 and to_string/1.

Summary

Types

A refusal: faults with their paths.

An affine matrix {a, b, c, d, e, f} for [a c e; b d f; 0 0 1]: a point maps as x' = a·x + c·y + e, y' = b·x + d·y + f.

One transform operation (spec/10 §1.3).

t()

Functions

An affine matrix applied to a point {x, y}.

The operations of first then those of second.

Whether the transform has no operation.

The transform followed by the matrix (a, b, c, d, e, f).

The product m1 · m2 of two affine matrices: applying it to a point applies m2 and then m1.

An empty transform.

A rotation by angle degrees about the origin.

The transform followed by a rotation by angle degrees about the origin.

The transform followed by a rotation by angle degrees about (cx, cy).

A uniform scale by s.

With two numbers, a scale by sx and sy; with a transform and a number, the transform followed by a uniform scale.

The transform followed by a scale by sx and sy.

The transform followed by a skew along x by angle degrees.

The transform followed by a skew along y by angle degrees.

The operations folded into one affine matrix, M = Op₁ · Op₂ · … · Opₙ, so the last operation applies to a point first, exactly as the transform attribute does; rotate(a, cx, cy) is translate(cx, cy) rotate(a) translate(-cx, -cy).

The SVG transform attribute's text: the operations joined by spaces.

A translation by (x, y).

The transform followed by a translation by (x, y).

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

matrix()

@type matrix() :: {number(), number(), number(), number(), number(), number()}

An affine matrix {a, b, c, d, e, f} for [a c e; b d f; 0 0 1]: a point maps as x' = a·x + c·y + e, y' = b·x + d·y + f.

operation()

@type operation() ::
  {:translate, number(), number()}
  | {:rotate, number()}
  | {:rotate, number(), number(), number()}
  | {:scale, number(), number()}
  | {:skew_x, number()}
  | {:skew_y, number()}
  | {:matrix, number(), number(), number(), number(), number(), number()}

One transform operation (spec/10 §1.3).

t()

@type t() :: %Macaw.Draw.IR.Transform{operations: [operation()]}

Functions

apply_matrix(m, point)

@spec apply_matrix(matrix(), {number(), number()}) :: {number(), number()} | error()

An affine matrix applied to a point {x, y}.

concat(first, second)

@spec concat(t(), t()) :: t() | error()

The operations of first then those of second.

empty?(arg1)

@spec empty?(t()) :: boolean() | error()

Whether the transform has no operation.

matrix(t, a, b, c, d, e, f)

@spec matrix(t(), number(), number(), number(), number(), number(), number()) ::
  t() | error()

The transform followed by the matrix (a, b, c, d, e, f).

multiply(m1, m2)

@spec multiply(matrix(), matrix()) :: matrix() | error()

The product m1 · m2 of two affine matrices: applying it to a point applies m2 and then m1.

new()

@spec new() :: t()

An empty transform.

rotate(angle)

@spec rotate(number()) :: t() | error()

A rotation by angle degrees about the origin.

rotate(t, angle)

@spec rotate(t(), number()) :: t() | error()

The transform followed by a rotation by angle degrees about the origin.

rotate(t, angle, cx, cy)

@spec rotate(t(), number(), number(), number()) :: t() | error()

The transform followed by a rotation by angle degrees about (cx, cy).

scale(s)

@spec scale(number()) :: t() | error()

A uniform scale by s.

scale(sx, sy)

@spec scale(number() | t(), number()) :: t() | error()

With two numbers, a scale by sx and sy; with a transform and a number, the transform followed by a uniform scale.

scale(t, sx, sy)

@spec scale(t(), number(), number()) :: t() | error()

The transform followed by a scale by sx and sy.

skew_x(t, angle)

@spec skew_x(t(), number()) :: t() | error()

The transform followed by a skew along x by angle degrees.

skew_y(t, angle)

@spec skew_y(t(), number()) :: t() | error()

The transform followed by a skew along y by angle degrees.

to_matrix(arg1)

@spec to_matrix(t()) :: matrix() | error()

The operations folded into one affine matrix, M = Op₁ · Op₂ · … · Opₙ, so the last operation applies to a point first, exactly as the transform attribute does; rotate(a, cx, cy) is translate(cx, cy) rotate(a) translate(-cx, -cy).

iex> Macaw.Draw.IR.Transform.translate(10, 20) |> Macaw.Draw.IR.Transform.to_matrix()
{1.0, 0.0, 0.0, 1.0, 10.0, 20.0}

to_string(arg1)

@spec to_string(t()) :: String.t() | error()

The SVG transform attribute's text: the operations joined by spaces.

iex> Macaw.Draw.IR.Transform.translate(22.5, 0) |> Macaw.Draw.IR.Transform.to_string()
"translate(22.5,0)"

translate(x, y)

@spec translate(number(), number()) :: t() | error()

A translation by (x, y).

translate(t, x, y)

@spec translate(t(), number(), number()) :: t() | error()

The transform followed by a translation by (x, y).