# AffineMatrix

A column-major matrix representing a two-dimensional affine transform.

An affine transformation is a linear transformation that preseves parallel lines.

See [https://en.wikipedia.org/wiki/Affine\_transformation](https://en.wikipedia.org/wiki/Affine_transformation)

## Constructors

AffineMatrix(a,b,c,d,tx,ty)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Creates a matrix of the form:

```
|a  c  tx|
|b  d  ty|
|0  0  1 |
```

`a`

`number`

`1`

x component of the first basis vector

`b`

`number`

`0`

y component of the first basis vector

`c`

`number`

`0`

x component of the second basis vector

`d`

`number`

`1`

y component of the second basis vector

`tx`

`number`

`0`

translation on the x axis

`ty`

`number`

`0`

translation on the y axis

AffineMatrix.fromTransform(transform)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Constructs an affine matrix from a transform object.

`transform`

`[TransformArgs](https://cuttle.xyz/learn/reference/interfaces#TransformArgs)`

AffineMatrix.fromTranslation(translation)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Constructs a translation matrix.

`translation`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

AffineMatrix.fromTranslationPoints(p1,p2)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Constructs a translation matrix from `p1` to `p2`.

`p1`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

A position to translate from

`p2`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

A position to translate to

AffineMatrix.fromRotation(angle)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Constructs a rotation matrix.

`angle`

`number`

The angle to rotate by (in degrees)

AffineMatrix.fromScale(scale)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Constructs a scale matrix.

`scale`

`(number | [Vec](https://cuttle.xyz/learn/reference/Vec))`

The scaling factor. Can be either a 2d vector or a scalar number.

AffineMatrix.fromCenterScale(center,scale)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Constructs a matrix that scales relative to a center point.

`center`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The center of the scale transformation. This point will not move

`scale`

`(number | [Vec](https://cuttle.xyz/learn/reference/Vec))`

The scaling factor. Can be either a 2d vector or a scalar

## Methods

.copy(m)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Copies the value of another matrix `m`.

`m`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.invert()→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Inverts the matrix.

.mul(m)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Multiplies the matrix by another matrix `m`.

```
m1.mul(m2); // m1 = m1 * m2
```

`m`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.mulWithoutTranslation(m)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Multiplies only the 2x2 portion portion of this matrix by another matrix `m`. Translation is discarded.

`m`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.preMul(m)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Multiplies another matrix `m` by this matrix, and then stores the result.

```
m1.preMul(m2); // m1 = m2 * m1
```

`m`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.preMulWithoutTranslation(m)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Multiplies only the 2x2 portion portion of another matrix by this matrix, and then stores the result. Translation is discarded.

`m`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

.translate(v)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Translates the matrix by a vector `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.scale(v)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Scales the matrix by a vector `v`.

`v`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.scaleScalar(s)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Scales the matrix by a uniformally by `s`.

`s`

`number`

.normalize()→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Scales the basis vectors of the matrix so that they have unit length.

Basis vectors of length 0 will not be changed.

.rotate(angle)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Rotates the matrix by an `angle`.

`angle`

`number`

An angle in degrees

.skew(angle)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Skews the "Y" basis vector of the matrix by an `angle`.

`angle`

`number`

An angle in degrees

.origin(origin)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)chainable

Translates the matrix such that the center of future scale, rotate and skew transformations will be `origin`.

`origin`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

.ensureMinimumBasisLength(length)→[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)

Ensure that the basis vectors of this matrix are at least as long as the specified length. If a vector is shorter than the specified length it will be set perpendicular to the opposing vector. If both are shorter they will be set to the identity matrix scaled by length.

`length`

`number`

The minimum length the basis vectors will have after this method is called

.determinant()→number

Returns The determinant of the matrix.

.equals(m)→boolean

`m`

`[AffineMatrix](https://cuttle.xyz/learn/reference/AffineMatrix)`

Returns `true` if this matrix _exactly_ equals the matrix `m`, `false` otherwise.

.isOrthogonal(tolerance)→boolean

Returns `true` if the basis vectors of the matrix are orthogonal.

`tolerance`

`number`

`DEFAULT_TOLERANCE`

.isInvertible()→boolean

Returns `true` if the matrix can be inverted.

.isUniformScale(tolerance)→boolean

`tolerance`

`number`

`DEFAULT_TOLERANCE`

Returns `true` if the basis vectors of the matrix are the same length.

.isMirror()→boolean

Returns `true` if the matrix mirrors space in either axis.

.isIdentity()→boolean

Returns `true` if the matrix is the identity.

Applying an identity matrix has no effect.

.isNaN()→boolean

Returns `true` if any component of the matrix is `NaN`.

.isFinite()→boolean

Returns `true` if all components of the matrix are finite numbers (not `Infinity` or `NaN`).

.isValid()→boolean

Returns `true` if the matrix is finite, and all of its components are numbers.

.toTransform()→[Transform](https://cuttle.xyz/learn/reference/interfaces#Transform)

Returns a transform `{position, rotation, scale, skew}` such that `AffineMatrix.fromTransform(transform)` will return the original matrix. It guarantees:

-   `0 <= rotation < 360`
-   `-90 < skew < 90` (assuming the matrix basis vectors are not collinear)

Notes:

-   This will return only one of two possible solutions. You can get the other one by negating `scale` and rotating by `180` degrees.
-   If either of the basis vectors are degenerate (close to zero length), then this will set `skew` to `0`.
-   If both of the basis vectors are degenerate, this will set `rotation` to `0`.
-   `scale` will always be returned as a `Vec`.

.toTransformWithOrigin(origin)→[TransformWithOrigin](https://cuttle.xyz/learn/reference/interfaces#TransformWithOrigin)

Similar to `.toTransform()`, except returns a transform with an additional `origin` property.

`origin`

`[Vec](https://cuttle.xyz/learn/reference/Vec)`

The origin of the resulting transform