# `Macaw.Draw.IR.Transform`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/ir/transform.ex#L1)

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`.

# `error`

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

A refusal: faults with their paths.

# `matrix`

```elixir
@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`

```elixir
@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`

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

# `apply_matrix`

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

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

# `concat`

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

The operations of `first` then those of `second`.

# `empty?`

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

Whether the transform has no operation.

# `matrix`

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

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

# `multiply`

```elixir
@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`

```elixir
@spec new() :: t()
```

An empty transform.

# `rotate`

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

A rotation by `angle` degrees about the origin.

# `rotate`

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

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

# `rotate`

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

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

# `scale`

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

A uniform scale by `s`.

# `scale`

```elixir
@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`

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

The transform followed by a scale by `sx` and `sy`.

# `skew_x`

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

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

# `skew_y`

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

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

# `to_matrix`

```elixir
@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`

```elixir
@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`

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

A translation by `(x, y)`.

# `translate`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
