# base/transformmatrix (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/transformmatrix/

2D affine transform matrix. (the same layout as Canvas2D's a, b, c, d, e, f)
| a c e |
| b d f |
| 0 0 1 |
- The original base/transformmatrix.js. The original's a ~ f read properties are public fields, and clone is replaced by copying (Copy).
- invert and identity were made first in the Rust version.

## TransformMatrix (struct) (Clone, Copy, Debug, PartialEq, Default)

2D affine transform matrix. (a Copy value with a ~ f as public fields, defaults to the identity matrix)
- translate, rotate and scale change the matrix itself and multiply on the right. multiply returns a new matrix.

Example: Translate and scale, then transform a point and invert

```rust
use vanilla::{TransformMatrix, Vector2};

let mut transform = TransformMatrix::identity();
transform.translate(100.0, 50.0);
transform.scale(2.0, 2.0);
let point = transform.transform_point(10.0, 5.0);
assert_eq!(point, Vector2::create(120.0, 60.0));

let inverse = transform.invert().unwrap();
assert_eq!(inverse.transform_point(120.0, 60.0), Vector2::create(10.0, 5.0));
```

- `pub a: f64` (field, since 0.3.2): x of the x axis vector. (new x = a * x + c * y + e)
  - returns f64
- `pub b: f64` (field, since 0.3.2): y of the x axis vector. (new y = b * x + d * y + f)
  - returns f64
- `pub c: f64` (field, since 0.3.2): x of the y axis vector.
  - returns f64
- `pub d: f64` (field, since 0.3.2): y of the y axis vector.
  - returns f64
- `pub e: f64` (field, since 0.3.2): Translation x.
  - returns f64
- `pub f: f64` (field, since 0.3.2): Translation y.
  - returns f64
- `pub const fn identity() -> TransformMatrix` (static, since 0.3.2): Identity matrix.
  - returns TransformMatrix: A matrix with a = d = 1 and the rest 0.
- `pub fn set_identity(&mut self)` (method, since 0.3.2): Resets to the identity matrix.
- `pub fn set_transform(&mut self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64)` (method, since 0.3.2): Sets the values directly.
  - `a` f64: x of the x axis vector.
  - `b` f64: y of the x axis vector.
  - `c` f64: x of the y axis vector.
  - `d` f64: y of the y axis vector.
  - `e` f64: Translation x.
  - `f` f64: Translation y.
- `pub fn copy_from(&mut self, other: &TransformMatrix)` (method, since 0.4.0): Copies the values of another matrix.
  - `other` &TransformMatrix: Matrix to copy from.
- `pub fn translate(&mut self, x: f64, y: f64)` (method, since 0.3.2): Accumulates a translation. (multiplies on the right)
  - `x` f64: x translation. (in the current transform's local coordinates)
  - `y` f64: y translation. (in the current transform's local coordinates)
- `pub fn rotate(&mut self, radian: f64)` (method, since 0.3.2): Accumulates a rotation. (radians, multiplies on the right)
  - `radian` f64: Rotation angle. (radians, positive turns +X toward +Y, does nothing if 0)

  Example: Rotating 90 degrees moves +X to +Y

  ```rust
  use vanilla::{math, TransformMatrix};
  
  let mut transform = TransformMatrix::identity();
  transform.rotate(math::PI * 0.5);
  let point = transform.transform_point(1.0, 0.0);
  assert!(point.x.abs() < 1e-6);
  assert!((point.y - 1.0).abs() < 1e-6);
  ```

- `pub fn scale(&mut self, x: f64, y: f64)` (method, since 0.3.2): Accumulates a scale. (multiplies on the right)
  - `x` f64: x scale. (1 leaves it unchanged)
  - `y` f64: y scale. (1 leaves it unchanged)
- `pub fn multiply(&self, other: &TransformMatrix) -> TransformMatrix` (method, since 0.3.2): Matrix product. (self * other)
  - `other` &TransformMatrix: Matrix to multiply on the right. (other applies to points first)
  - returns TransformMatrix: A new matrix equal to self x other. (self is not changed)

  Example: Multiply a child transform onto a parent transform

  ```rust
  use vanilla::{TransformMatrix, Vector2};
  
  let mut parent = TransformMatrix::identity();
  parent.translate(100.0, 0.0);
  let mut child = TransformMatrix::identity();
  child.scale(2.0, 2.0);
  let world = parent.multiply(&child);
  assert_eq!(world.transform_point(10.0, 10.0), Vector2::create(120.0, 20.0));
  ```

- `pub fn invert(&self) -> Option<TransformMatrix>` (method, since 0.3.2): Inverse matrix. (None if the determinant is 0)
  - returns Option<TransformMatrix>: The inverse matrix. None if the determinant (a * d - b * c) is 0.

  Example: Cannot be inverted if one axis scale is 0

  ```rust
  use vanilla::TransformMatrix;
  
  let mut flat = TransformMatrix::identity();
  flat.scale(0.0, 1.0);
  assert!(flat.invert().is_none());
  assert!(TransformMatrix::identity().invert().is_some());
  ```

- `pub fn transform_point(&self, x: f64, y: f64) -> Vector2` (method, since 0.3.2): Transforms a point.
  - `x` f64: x of the point.
  - `y` f64: y of the point.
  - returns Vector2: The transformed point. (a * x + c * y + e, b * x + d * y + f)
- `pub fn get_maximum_scale(&self) -> f64` (method, since 0.3.2): Scale factor of the larger of the two axes.
  - returns f64: The larger of the lengths of the x axis vector (a, b) and the y axis vector (c, d).
- `pub fn write_to_float32_array(&self, array: &mut [f32; 9])` (method, since 0.4.0): Writes to a shader mat3 (column-major) array. (the original writeToFloat32Array)
  - `array` &mut [f32; 9]: Array to copy into. (9 column-major values: a, b, 0, c, d, 0, e, f, 1)

  Example: A translation matrix into a mat3 array

  ```rust
  use vanilla::TransformMatrix;
  
  let mut transform = TransformMatrix::identity();
  transform.translate(3.0, 4.0);
  let mut array = [0.0f32; 9];
  transform.write_to_float32_array(&mut array);
  assert_eq!(array, [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 3.0, 4.0, 1.0]);
  ```

