# base/transformmatrix (vanilla-engine 0.4.0)

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

2D 아핀 변환 행렬. (Canvas2D 의 a, b, c, d, e, f 와 같은 배치)
| a c e |
| b d f |
| 0 0 1 |
- 원본 base/transformmatrix.js. 원본의 a ~ f 읽기 프로퍼티는 공개 필드로, clone 은 복사(Copy)로 갈음한다.
- invert 와 identity 는 러스트 판에서 먼저 만들었다.

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

2D 아핀 변환 행렬. (a ~ f 를 공개 필드로 둔 Copy 값, 기본값은 단위 행렬)
- translate, rotate, scale 은 자신을 바꾸고 오른쪽에 곱한다. multiply 는 새 행렬을 돌려준다.

예제: 옮기고 키운 뒤 점 변환과 역변환

```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 축 벡터의 x. (새 x = a * x + c * y + e)
  - returns f64
- `pub b: f64` (field, since 0.3.2): x 축 벡터의 y. (새 y = b * x + d * y + f)
  - returns f64
- `pub c: f64` (field, since 0.3.2): y 축 벡터의 x.
  - returns f64
- `pub d: f64` (field, since 0.3.2): y 축 벡터의 y.
  - returns f64
- `pub e: f64` (field, since 0.3.2): 이동 x.
  - returns f64
- `pub f: f64` (field, since 0.3.2): 이동 y.
  - returns f64
- `pub const fn identity() -> TransformMatrix` (static, since 0.3.2): 단위 행렬.
  - returns TransformMatrix: a = d = 1, 나머지가 0 인 행렬.
- `pub fn set_identity(&mut self)` (method, since 0.3.2): 단위 행렬로 초기화.
- `pub fn set_transform(&mut self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64)` (method, since 0.3.2): 값 직접 설정.
  - `a` f64: x 축 벡터의 x.
  - `b` f64: x 축 벡터의 y.
  - `c` f64: y 축 벡터의 x.
  - `d` f64: y 축 벡터의 y.
  - `e` f64: 이동 x.
  - `f` f64: 이동 y.
- `pub fn copy_from(&mut self, other: &TransformMatrix)` (method, since 0.4.0): 다른 행렬 값 복사.
  - `other` &TransformMatrix: 복사해 올 행렬.
- `pub fn translate(&mut self, x: f64, y: f64)` (method, since 0.3.2): 이동 누적. (오른쪽에 곱한다)
  - `x` f64: x 이동량. (지금 변환의 로컬 좌표)
  - `y` f64: y 이동량. (지금 변환의 로컬 좌표)
- `pub fn rotate(&mut self, radian: f64)` (method, since 0.3.2): 회전 누적. (라디안, 오른쪽에 곱한다)
  - `radian` f64: 회전 각. (라디안, 양수면 +X 를 +Y 쪽으로 돌린다, 0 이면 아무것도 하지 않는다)

  예제: 90 도 돌리면 +X 가 +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): 크기 누적. (오른쪽에 곱한다)
  - `x` f64: x 배율. (1 이면 그대로)
  - `y` f64: y 배율. (1 이면 그대로)
- `pub fn multiply(&self, other: &TransformMatrix) -> TransformMatrix` (method, since 0.3.2): 행렬 곱. (self * other)
  - `other` &TransformMatrix: 오른쪽에 곱할 행렬. (점에는 other 가 먼저 걸린다)
  - returns TransformMatrix: self x other 인 새 행렬. (자신은 바뀌지 않는다)

  예제: 부모 변환에 자식 변환 곱하기

  ```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): 역행렬. (행렬식이 0 이면 None)
  - returns Option<TransformMatrix>: 역행렬. 행렬식(a * d - b * c)이 0 이면 None.

  예제: 한 축 배율이 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): 점 변환.
  - `x` f64: 점의 x.
  - `y` f64: 점의 y.
  - returns Vector2: 변환한 점. (a * x + c * y + e, b * x + d * y + f)
- `pub fn get_maximum_scale(&self) -> f64` (method, since 0.3.2): 두 축 가운데 큰 쪽의 크기 배율.
  - returns f64: x 축 벡터(a, b)와 y 축 벡터(c, d)의 길이 가운데 큰 값.
- `pub fn write_to_float32_array(&self, array: &mut [f32; 9])` (method, since 0.4.0): 셰이더 mat3 (열 우선) 배열로 기록. (원본 writeToFloat32Array)
  - `array` &mut [f32; 9]: 옮겨 쓸 배열. (열 우선 9 개: a, b, 0, c, d, 0, e, f, 1)

  예제: 이동 행렬을 mat3 배열로

  ```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]);
  ```

