Skip to content

Visualize Your Ideas With Code

feat(core): add Matrix2D type (#340)

Kai Sassnowski66b41e6beaca
diff --git a/packages/core/src/types/Matrix.ts b/packages/core/src/types/Matrix.ts
index be41e943..c0166d08 100644
--- a/packages/core/src/types/Matrix.ts
+++ b/packages/core/src/types/Matrix.ts
@@ -1,5 +1,7 @@
import {Vector2} from './Vector';
+export const EPSILON = 0.000001;
+
export function transformAngle(angle: number, matrix: DOMMatrix) {
const radians = (angle / 180) * Math.PI;
const vector = Vector2.fromRadians(radians).transform(matrix);
diff --git a/packages/core/src/types/Matrix2D.test.ts b/packages/core/src/types/Matrix2D.test.ts
new file mode 100644
index 00000000..b863a4d7
--- /dev/null
+++ b/packages/core/src/types/Matrix2D.test.ts
@@ -0,0 +1,384 @@
+import {beforeEach, describe, expect, test} from 'vitest';
+import {Matrix2D} from './Matrix2D';
+import {Vector2, PossibleVector2} from './Vector';
+
+type Matrix2DValues = [number, number, number, number, number, number];
+
+describe('Matrix2D', () => {
+ let a: Matrix2D;
+ let b: Matrix2D;
+ let c: Matrix2D;
+
+ beforeEach(() => {
+ a = new Matrix2D(1, 2, 3, 4, 5, 6);
+ b = new Matrix2D(7, 8, 9, 10, 11, 12);
+ c = new Matrix2D(1, 2, 1, 2, 1, 2);
+ });
+
+ describe('construction', () => {
+ test('can be constructed from an existing matrix', () => {
+ const matrix = new Matrix2D(a);
+ expect(matrix.values).toEqual(a.values);
+ });
+
+ test('can be constructed from an array of three PossibleVector2', () => {
+ const matrix = new Matrix2D([[1, 0], {x: 0, y: 1}, Vector2.zero]);
+ expect(matrix.values).toEqual(new Float32Array([1, 0, 0, 1, 0, 0]));
+ });
+
+ test('can be constructed from individual numbers', () => {
+ const matrix = new Matrix2D(1, 0, 0, 0, 1, 0);
+ expect(matrix.values).toEqual(new Float32Array([1, 0, 0, 0, 1, 0]));
+ });
+
+ test('can be constructed from a three sets of two numbers', () => {
+ const matrix = new Matrix2D([
+ [1, 0],
+ [0, 1],
+ [0, 0],
+ ]);
+ expect(matrix.values).toEqual(new Float32Array([1, 0, 0, 1, 0, 0]));
+ });
+
+ test('can be constructed from array of six numbers', () => {
+ const matrix = new Matrix2D([1, 0, 0, 0, 1, 0]);
+ expect(matrix.values).toEqual(new Float32Array([1, 0, 0, 0, 1, 0]));
+ });
+
+ test.each([
+ [
+ '[number, number]',
+ [
+ [1, 0],
+ [0, 0],
+ [1, 0],
+ ],
+ [1, 0, 0, 0, 1, 0],
+ ],
+ [
+ 'SerializedVector2',
+ [
+ {x: 1, y: 1},
+ {x: 0, y: 0},
+ {x: 0, y: 1},
+ ],
+ [1, 1, 0, 0, 0, 1],
+ ],
+ [
+ '{width: T, height: T}',
+ [
+ {width: 0, height: 1},
+ {width: 1, height: 1},
+ {width: 1, height: 1},
+ ],
+ [0, 1, 1, 1, 1, 1],
+ ],
+ [
+ 'Vector2',
+ [new Vector2(0, 1), new Vector2(1, 1), new Vector2(10, 10)],
+ [0, 1, 1, 1, 10, 10],
+ ],
+ ['number', [1, 0, 0], [1, 1, 0, 0, 0, 0]],
+ ['undefined', [undefined, undefined, undefined], [0, 0, 0, 0, 0, 0]],
+ ] as [string, [PossibleVector2, PossibleVector2, PossibleVector2], Matrix2DValues][])(
+ 'can be constructed from three PossibleVector2 %s',
+ (_, [vec1, vec2, vec3], expected) => {
+ const matrix = new Matrix2D(vec1, vec2, vec3);
+ expect(matrix.values).toEqual(new Float32Array(expected));
+ },
+ );
+
+ test('can be constructed from a rotation', () => {
+ const matrix = Matrix2D.fromRotation(90);
+ expect(matrix.equals(new Matrix2D(0, 1, -1, 0, 0, 0))).toBe(true);
+ });
+
+ test.each([
+ ['non-uniform', [-100, 200], [1, 0, 0, 1, -100, 200]],
+ ['uniform', 400, [1, 0, 0, 1, 400, 400]],
+ ] as [string, PossibleVector2, Matrix2DValues][])(
+ 'can be constructed from a translation: %s',
+ (_, translation, expected) => {
+ const matrix = Matrix2D.fromTranslation(translation);
+ expect(matrix.equals(new Matrix2D(...expected))).toBe(true);
+ },
+ );
+
+ test.each([
+ ['non-uniform', [2, 3], [2, 0, 0, 3, 0, 0]],
+ ['uniform', 3, [3, 0, 0, 3, 0, 0]],
+ ] as [string, PossibleVector2, Matrix2DValues][])(
+ 'can be constructed from a scaling: %s',
+ (_, scaling, expected) => {
+ const matrix = Matrix2D.fromScaling(scaling);
+ expect(matrix).toEqual(new Matrix2D(...expected));
+ },
+ );
+ });
+
+ describe('getters and setters', () => {
+ test('can access the x component vector', () => {
+ expect(a.x).toEqual(new Vector2(1, 2));
+ });
+
+ test('can access the y component vector', () => {
+ expect(a.y).toEqual(new Vector2(3, 4));
+ });
+
+ test('can access the horizontal scaling', () => {
+ expect(a.scaleX).toEqual(1);
+ });
+
+ test('can set only the horizontal scaling', () => {
+ const matrix = new Matrix2D();
+ matrix.scaleX = 5;
+ expect(matrix).toEqual(new Matrix2D(5, 0, 0, 1, 0, 0));
+ });
+
+ test('can access the vertical scaling', () => {
+ expect(a.scaleY).toEqual(4);
+ });
+
+ test('can set only the vertical scaling', () => {
+ const matrix = new Matrix2D();
+ matrix.scaleY = 5;
+ expect(matrix).toEqual(new Matrix2D(1, 0, 0, 5, 0, 0));
+ });
+
+ test('can set the scaling', () => {
+ const matrix = new Matrix2D(1, 0, 0, 1, 0, 0);
+ matrix.scaling = [2, 5];
+ expect(matrix).toEqual(new Matrix2D(2, 0, 0, 5, 0, 0));
+ });
+
+ test('can access scaling', () => {
+ expect(a.scaling).toEqual(new Vector2(1, 4));
+ });
+
+ test('can access the horizontal translation', () => {
+ expect(a.translateX).toEqual(5);
+ });
+
+ test('can set only the horizontal translation', () => {
+ a.translateX = -5;
+ expect(a).toEqual(new Matrix2D(1, 2, 3, 4, -5, 6));
+ });
+
+ test('can access the vertical translation', () => {
+ expect(a.translateY).toEqual(6);
+ });
+
+ test('can set only the vertical translation', () => {
+ a.translateY = 20;
+ expect(a).toEqual(new Matrix2D(1, 2, 3, 4, 5, 20));
+ });
+
+ test('can access the translation', () => {
+ expect(a.translation).toEqual(new Vector2(5, 6));
+ });
+
+ test('can set the translation', () => {
+ a.translation = [-5, 20];
+ expect(a).toEqual(new Matrix2D(1, 2, 3, 4, -5, 20));
+ });
+
+ test('can set the rotation', () => {
+ const matrix = new Matrix2D();
+ matrix.rotation = 90;
+ expect(matrix.equals(new Matrix2D(0, 1, -1, 0, 0, 0))).toBe(true);
+ });
+
+ test('can access the rotation', () => {
+ const matrix = new Matrix2D(0, 1, -1, 0, 0, 0);
+ expect(matrix.rotation).toBe(90);
+ });
+ });
+
+ describe('matrix properties', () => {
+ test('A + B = B + A', () => {
+ expect(a.add(b).equals(b.add(a))).toBe(true);
+ });
+
+ test('A + (B + C) = (A + B) + A', () => {
+ expect(a.add(b.add(c)).equals(a.add(b).add(c))).toBe(true);
+ });
+
+ test('A + 0 = A', () => {
+ expect(a.add(Matrix2D.zero).equals(a)).toBe(true);
+ });
+
+ test('r(A + B) = rA + rB', () => {
+ const result1 = a.add(b).mulScalar(2);
+ const result2 = a.mulScalar(2).add(b.mulScalar(2));
+ expect(result1).toEqual(result2);
+ });
+
+ test('(r + s)A = rA + sA', () => {
+ const result1 = a.mulScalar(2 + 5);
+ const result2 = a.mulScalar(2).add(a.mulScalar(5));
+ expect(result1).toEqual(result2);
+ });
+
+ test('A(BC) = (AB)C', () => {
+ const result1 = a.mul(b.mul(c));
+ const result2 = a.mul(b).mul(c);
+ expect(result1.equals(result2)).toBe(true);
+ });
+
+ test('(B + C)A = BA + CA', () => {
+ const result1 = b.add(c).mul(a);
+ const result2 = b.mul(a).add(c.mul(a));
+ expect(result1).toEqual(result2);
+ });
+
+ test('IA = A = AI', () => {
+ const result1 = Matrix2D.identity.mul(a);
+ const result2 = a.mul(Matrix2D.identity);
+ expect(result1).toEqual(a);
+ expect(result2).toEqual(a);
+ });
+
+ test('AA^-1 = A^-1A = I', () => {
+ const result1 = a.mul(a.inverse!);
+ const result2 = a.inverse!.mul(a);
+ expect(result1).toEqual(Matrix2D.identity);
+ expect(result2).toEqual(Matrix2D.identity);
+ });
+
+ test('(AB)^-1 = B^-1 A^-1', () => {
+ const result1 = a.mul(b).inverse!;
+ const result2 = b.inverse!.mul(a.inverse!);
+ // Using Matrix2D.equals here as JavaScript thinks 0 and -0 are different values...
+ expect(result1.equals(result2));
+ });
+
+ test('I^-1 = I', () => {
+ expect(Matrix2D.identity.inverse?.equals(Matrix2D.identity)).toBe(true);
+ });
+
+ test('(A^-1)^-1 = A', () => {
+ expect(a.inverse?.inverse).toEqual(a);
+ });
+ });
+
+ describe('access', () => {
+ test('can access the component vectors by index', () => {
+ expect(Matrix2D.identity.column(0)).toEqual(new Vector2(1, 0));
+ expect(Matrix2D.identity.column(1)).toEqual(new Vector2(0, 1));
+ expect(Matrix2D.identity.column(2)).toEqual(new Vector2(0, 0));
+ });
+
+ test('can access rows by index', () => {
+ expect(Matrix2D.identity.row(0)).toEqual([1, 0, 0]);
+ expect(Matrix2D.identity.row(1)).toEqual([0, 1, 0]);
+ });
+ });
+
+ describe('equality', () => {
+ test('equal when all values have exactly equal', () => {
+ expect(Matrix2D.identity.equals(Matrix2D.identity)).toBe(true);
+ });
+
+ test('equal when all values are within threshold of each other', () => {
+ const other = new Matrix2D(0.5, -0.5, 0.5, 0.5, 0.5, 0.5);
+ expect(Matrix2D.identity.equals(other, 0.5)).toBe(true);
+ });
+
+ test('not equal when not all values are within EPSILON of each other', () => {
+ const other = new Matrix2D(0.4, -0.4, 0.4, 0.4, 0.4, 0.4);
+ expect(Matrix2D.identity.equals(other, 0.5)).toBe(false);
+ });
+
+ test('exactly equal when all values have exactly equal', () => {
+ expect(Matrix2D.identity.exactlyEquals(Matrix2D.identity)).toBe(true);
+ });
+
+ test('not exactly equal when not all values are exactly equal', () => {
+ // prettier-ignore
+ const other = new Matrix2D(
+ 0.999999, -0.999999,
+ 0.999999, 0.999999,
+ 0.999999, -0.999999,
+ );
+ expect(Matrix2D.identity.exactlyEquals(other)).toBe(false);
+ });
+ });
+
+ describe('operations', () => {
+ test("can calculate the matrix' inverse", () => {
+ expect(a.inverse).toEqual(new Matrix2D(-2, 1, 1.5, -0.5, 1, -2));
+ });
+
+ test('returns null if the matrix is not invertible', () => {
+ expect(c.inverse).toBeNull();
+ });
+
+ test("can calculate the matrix' determinant", () => {
+ expect(a.determinant).toBe(-2);
+ });
+
+ test('can multiply two Matrix2D', () => {
+ expect(a.mul(b)).toEqual(new Matrix2D(31, 46, 39, 58, 52, 76));
+ });
+
+ test('can rotate a matrix by radians', () => {
+ const result = a.rotate(Math.PI * 0.5, false);
+ expect(result.equals(new Matrix2D(3, 4, -1, -2, 5, 6))).toBe(true);
+ });
+
+ test('can rotate a matrix by degrees', () => {
+ const result = a.rotate(90);
+ expect(result.equals(new Matrix2D(3, 4, -1, -2, 5, 6)));
+ });
+
+ test('can scale a matrix by a vector', () => {
+ expect(a.scale([2, 3])).toEqual(new Matrix2D(2, 4, 9, 12, 5, 6));
+ });
+
+ test('can scale a matrix uniformly by a scalar', () => {
+ expect(a.scale(2)).toEqual(new Matrix2D(2, 4, 6, 8, 5, 6));
+ });
+
+ test('can multiply with a scalar', () => {
+ expect(a.mulScalar(2)).toEqual(new Matrix2D(2, 4, 6, 8, 10, 12));
+ });
+
+ test('can translate a matrix by a vector', () => {
+ expect(a.translate([2, 3])).toEqual(new Matrix2D(1, 2, 3, 4, 16, 22));
+ });
+
+ test('can translate a matrix uniformly by a scalar', () => {
+ expect(a.translate(2)).toEqual(new Matrix2D(1, 2, 3, 4, 13, 18));
+ });
+
+ test('can add matrixes', () => {
+ expect(a.add(b)).toEqual(new Matrix2D(8, 10, 12, 14, 16, 18));
+ });
+
+ test('can subtract matrixes', () => {
+ expect(a.sub(b)).toEqual(new Matrix2D(-6, -6, -6, -6, -6, -6));
+ });
+
+ test('can scale a rotated matrix while keeping the rotation', () => {
+ const matrix = Matrix2D.fromRotation(45);
+ matrix.scaling = 2;
+
+ expect(matrix.rotation).toBe(45);
+ });
+
+ test('can set the rotation of a scaled matrix while keeping the scaling', () => {
+ const matrix = Matrix2D.fromScaling(2).rotate(45);
+ matrix.rotation = 90;
+
+ expect(matrix.equals(new Matrix2D(0, 2, -2, 0, 0, 0))).toBe(true);
+ });
+ });
+
+ test('defines identity matrix', () => {
+ expect(Matrix2D.identity).toEqual(new Matrix2D(1, 0, 0, 1, 0, 0));
+ });
+
+ test('defines zero matrix', () => {
+ expect(Matrix2D.zero).toEqual(new Matrix2D(0, 0, 0, 0, 0, 0));
+ });
+});
diff --git a/packages/core/src/types/Matrix2D.ts b/packages/core/src/types/Matrix2D.ts
new file mode 100644
index 00000000..e27d5252
--- /dev/null
+++ b/packages/core/src/types/Matrix2D.ts
@@ -0,0 +1,681 @@
+import {EPSILON} from './Matrix';
+import {PossibleVector2, Vector2} from './Vector';
+
+export type PossibleMatrix2D =
+ | Matrix2D
+ | [number, number, number, number, number, number]
+ | [PossibleVector2, PossibleVector2, PossibleVector2]
+ | undefined;
+
+/**
+ * A specialized 2x3 Matrix representing a 2D transformation.
+ *
+ * A Matrix2D contains six elements defined as
+ * [a, b,
+ * c, d,
+ * tx, ty]
+ *
+ * This is a shortcut for a 3x3 matrix of the form
+ * [a, b, 0,
+ * c, d, 0
+ * tx, ty, 1]
+ *
+ * Note that because a Matrix2D ignores the z-values of each component vectors,
+ * it does not satisfy all properties of a "real" 3x3 matrix.
+ *
+ * - A Matrix2D has no transpose
+ * - A(B + C) = AB + AC does not hold for a Matrix2D
+ * - (rA)^-1 = r^-1 A^-1, r != 0 does not hold for a Matrix2D
+ * - r(AB) = (rA)B = A(rB) does not hold for a Matrix2D
+ */
+export class Matrix2D {
+ public readonly values: Float32Array = new Float32Array(6);
+ public static readonly identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0);
+ public static readonly zero: Matrix2D = new Matrix2D(0, 0, 0, 0, 0, 0);
+
+ public static fromRotation(angle: number): Matrix2D {
+ return Matrix2D.identity.rotate(angle);
+ }
+
+ public static fromTranslation(
+ translation: PossibleVector2<number>,
+ ): Matrix2D {
+ return Matrix2D.identity.translate(new Vector2(translation));
+ }
+
+ public static fromScaling(scale: PossibleVector2<number>): Matrix2D {
+ return Matrix2D.identity.scale(new Vector2(scale));
+ }
+
+ public get x(): Vector2 {
+ return new Vector2(this.values[0], this.values[1]);
+ }
+
+ public get y(): Vector2 {
+ return new Vector2(this.values[2], this.values[3]);
+ }
+
+ public get scaleX(): number {
+ return this.values[0];
+ }
+
+ public set scaleX(value: number) {
+ this.values[0] = this.x.normalized.scale(value).x;
+ }
+
+ public set scaleY(value: number) {
+ this.values[3] = this.y.normalized.scale(value).y;
+ }
+
+ public get scaleY(): number {
+ return this.values[3];
+ }
+
+ public set translateX(value: number) {
+ this.values[4] = value;
+ }
+
+ public get translateX(): number {
+ return this.values[4];
+ }
+
+ public set translateY(value: number) {
+ this.values[5] = value;
+ }
+
+ public get translateY(): number {
+ return this.values[5];
+ }
+
+ public set rotation(angle: number) {
+ const result = this.rotate(angle - this.rotation);
+
+ this.values[0] = result.values[0];
+ this.values[1] = result.values[1];
+ this.values[2] = result.values[2];
+ this.values[3] = result.values[3];
+ }
+
+ public get rotation(): number {
+ return (Math.atan2(this.values[1], this.values[0]) * 180) / Math.PI;
+ }
+
+ public set translation(translation: PossibleVector2<number>) {
+ const vec = new Vector2(translation);
+ this.values[4] = vec.x;
+ this.values[5] = vec.y;
+ }
+
+ public get translation(): Vector2 {
+ return new Vector2(this.values[4], this.values[5]);
+ }
+
+ public set scaling(value: PossibleVector2<number>) {
+ const scale = new Vector2(value);
+
+ const x = new Vector2(this.values[0], this.values[1]).normalized;
+ const y = new Vector2(this.values[2], this.values[3]).normalized;
+
+ this.values[0] = x.x * scale.x;
+ this.values[1] = x.y * scale.y;
+ this.values[2] = y.x * scale.x;
+ this.values[3] = y.y * scale.y;
+ }
+
+ public get scaling(): Vector2 {
+ return new Vector2(this.values[0], this.values[3]);
+ }
+
+ /**
+ * Get the inverse of the matrix.
+ *
+ * @remarks
+ * If the matrix is not invertible, i.e. its determinant is `0`, this will
+ * return `null`, instead.
+ *
+ * @example
+ * ```ts
+ * const matrix = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ *
+ * const inverse = matrix.inverse;
+ * // => Matrix2D(
+ * // [-2, 1],
+ * // [1.5, -0.5],
+ * // [1, -2],
+ * // )
+ * ```
+ */
+ public get inverse(): Matrix2D | null {
+ const aa = this.values[0],
+ ab = this.values[1],
+ ac = this.values[2],
+ ad = this.values[3];
+ const atx = this.values[4],
+ aty = this.values[5];
+
+ let det = aa * ad - ab * ac;
+
+ if (!det) {
+ return null;
+ }
+
+ det = 1.0 / det;
+
+ return new Matrix2D(
+ ad * det,
+ -ab * det,
+ -ac * det,
+ aa * det,
+ (ac * aty - ad * atx) * det,
+ (ab * atx - aa * aty) * det,
+ );
+ }
+
+ /**
+ * Get the determinant of the matrix.
+ */
+ public get determinant(): number {
+ return this.values[0] * this.values[3] - this.values[1] * this.values[2];
+ }
+
+ public get domMatrix(): DOMMatrix {
+ return new DOMMatrix([
+ this.values[0],
+ this.values[1],
+ this.values[2],
+ this.values[3],
+ this.values[4],
+ this.values[5],
+ ]);
+ }
+
+ public constructor();
+ public constructor(matrix: PossibleMatrix2D);
+ public constructor(
+ x: PossibleVector2<number>,
+ y: PossibleVector2<number>,
+ z: PossibleVector2<number>,
+ );
+ public constructor(
+ a: number,
+ b: number,
+ c: number,
+ d: number,
+ tx: number,
+ ty: number,
+ );
+ public constructor(
+ a?: PossibleMatrix2D | PossibleVector2<number>,
+ b?: PossibleVector2<number>,
+ c?: PossibleVector2<number>,
+ d?: number,
+ tx?: number,
+ ty?: number,
+ ) {
+ if (arguments.length === 0) {
+ this.values = new Float32Array([1, 0, 0, 1, 0, 0]);
+ return;
+ }
+
+ if (arguments.length === 6) {
+ this.values[0] = a as number;
+ this.values[1] = b as number;
+ this.values[2] = c as number;
+ this.values[3] = d as number;
+ this.values[4] = tx as number;
+ this.values[5] = ty as number;
+ return;
+ }
+
+ if (a instanceof Matrix2D) {
+ this.values = a.values;
+ return;
+ }
+
+ if (Array.isArray(a)) {
+ if (a.length === 2) {
+ this.values[0] = a[0];
+ this.values[1] = a[1];
+ this.values[2] = (b as number[])[0];
+ this.values[3] = (b as number[])[1];
+ this.values[4] = (c as number[])[0];
+ this.values[5] = (c as number[])[1];
+ return;
+ }
+
+ if (a.length === 3) {
+ const x = new Vector2(a[0]);
+ const y = new Vector2(a[1]);
+ const z = new Vector2(a[2]);
+ this.values[0] = x.x;
+ this.values[1] = x.y;
+ this.values[2] = y.x;
+ this.values[3] = y.y;
+ this.values[4] = z.x;
+ this.values[5] = z.y;
+ return;
+ }
+
+ this.values[0] = a[0];
+ this.values[1] = a[1];
+ this.values[2] = a[2];
+ this.values[3] = a[3];
+ this.values[4] = a[4];
+ this.values[5] = a[5];
+ return;
+ }
+
+ const x = new Vector2(a as PossibleVector2<number>);
+ const y = new Vector2(b);
+ const z = new Vector2(c);
+ this.values[0] = x.x;
+ this.values[1] = x.y;
+ this.values[2] = y.x;
+ this.values[3] = y.y;
+ this.values[4] = z.x;
+ this.values[5] = z.y;
+ }
+
+ /**
+ * Get the nth component vector of the matrix. Only defined for 0, 1, and 2.
+ *
+ * @example
+ * ```ts
+ * const matrix = new Matrix2D(
+ * [1, 0],
+ * [0, 0],
+ * [1, 0],
+ * );
+ *
+ * const x = matrix.column(0);
+ * // Vector2(1, 0)
+ *
+ * const y = matrix.column(1);
+ * // Vector2(0, 0)
+ *
+ * const z = matrix.column(1);
+ * // Vector2(1, 0)
+ * ```
+ *
+ * @param index - The index of the component vector to retrieve.
+ */
+ public column(index: number): Vector2 {
+ return new Vector2(this.values[index * 2], this.values[index * 2 + 1]);
+ }
+
+ /**
+ * Returns the nth row of the matrix. Only defined for 0 and 1.
+ *
+ * @example
+ * ```ts
+ * const matrix = new Matrix2D(
+ * [1, 0],
+ * [0, 0],
+ * [1, 0],
+ * );
+ *
+ * const firstRow = matrix.column(0);
+ * // [1, 0, 1]
+ *
+ * const secondRow = matrix.column(1);
+ * // [0, 0, 0]
+ * ```
+ *
+ * @param index - The index of the row to retrieve.
+ */
+ public row(index: number): [number, number, number] {
+ return [this.values[index], this.values[index + 2], this.values[index + 4]];
+ }
+
+ /**
+ * Returns the matrix product of this matrix with the provided matrix.
+ *
+ * @remarks
+ * This method returns a new matrix representing the result of the
+ * computation. It will not modify the source matrix.
+ *
+ * @example
+ * ```ts
+ * const a = new Matrix2D(
+ * [1, 2],
+ * [0, 1],
+ * [1, 1],
+ * );
+ * const b = new Matrix2D(
+ * [2, 1],
+ * [1, 1],
+ * [1, 1],
+ * );
+ *
+ * const result = a.mul(b);
+ * // => Matrix2D(
+ * // [2, 5],
+ * // [1, 3],
+ * // [2, 4],
+ * // )
+ * ```
+ *
+ * @param other - The matrix to multiply with
+ */
+ public mul(other: Matrix2D): Matrix2D {
+ const a0 = this.values[0],
+ a1 = this.values[1],
+ a2 = this.values[2],
+ a3 = this.values[3],
+ a4 = this.values[4],
+ a5 = this.values[5];
+ const b0 = other.values[0],
+ b1 = other.values[1],
+ b2 = other.values[2],
+ b3 = other.values[3],
+ b4 = other.values[4],
+ b5 = other.values[5];
+
+ return new Matrix2D(
+ a0 * b0 + a2 * b1,
+ a1 * b0 + a3 * b1,
+ a0 * b2 + a2 * b3,
+ a1 * b2 + a3 * b3,
+ a0 * b4 + a2 * b5 + a4,
+ a1 * b4 + a3 * b5 + a5,
+ );
+ }
+
+ /**
+ * Rotate the matrix by the provided angle. By default, the angle is
+ * provided in degrees.
+ *
+ * @remarks
+ * This method returns a new matrix representing the result of the
+ * computation. It will not modify the source matrix.
+ *
+ * @example
+ * ```ts
+ * const a = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ *
+ * const result = a.rotate(90);
+ * // => Matrix2D(
+ * // [3, 4],
+ * // [-1, -2],
+ * // [5, 6],
+ * // )
+ *
+ * // Provide the angle in radians
+ * const result = a.rotate(Math.PI * 0.5, true);
+ * // => Matrix2D(
+ * // [3, 4],
+ * // [-1, -2],
+ * // [5, 6],
+ * // )
+ * ```
+ *
+ * @param angle - The angle by which to rotate the matrix
+ * @param radians - Whether the angle is provided in radians
+ */
+ public rotate(angle: number, degrees = true): Matrix2D {
+ if (degrees) {
+ angle = (angle * Math.PI) / 180;
+ }
+
+ const a0 = this.values[0],
+ a1 = this.values[1],
+ a2 = this.values[2],
+ a3 = this.values[3],
+ a4 = this.values[4],
+ a5 = this.values[5];
+ const s = Math.sin(angle);
+ const c = Math.cos(angle);
+
+ return new Matrix2D(
+ a0 * c + a2 * s,
+ a1 * c + a3 * s,
+ a0 * -s + a2 * c,
+ a1 * -s + a3 * c,
+ a4,
+ a5,
+ );
+ }
+
+ /**
+ * Scale the x and y component vectors of the matrix.
+ *
+ * @remarks
+ * If `vec` is provided as a vector, the x and y component vectors of the
+ * matrix will be scaled by the x and y parts of the vector, respectively.
+ *
+ * If `vec` is provided as a scalar, the x and y component vectors will be
+ * scaled uniformly by this factor.
+ *
+ * This method returns a new matrix representing the result of the
+ * computation. It will not modify the source matrix.
+ *
+ * @example
+ * ```ts
+ * const matrix = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ *
+ * const result1 = matrix.scale([2, 3]);
+ * // => new Matrix2D(
+ * // [2, 4],
+ * // [9, 12],
+ * // [5, 6],
+ * // )
+ *
+ * const result2 = matrix.scale(2);
+ * // => new Matrix2D(
+ * // [2, 4],
+ * // [6, 8],
+ * // [5, 6],
+ * // )
+ * ```
+ *
+ * @param vec - The factor by which to scale the matrix
+ */
+ public scale(vec: PossibleVector2): Matrix2D {
+ const v = new Vector2(vec);
+
+ return new Matrix2D(
+ this.values[0] * v.x,
+ this.values[1] * v.x,
+ this.values[2] * v.y,
+ this.values[3] * v.y,
+ this.values[4],
+ this.values[5],
+ );
+ }
+
+ /**
+ * Multiply each value of the matrix by a scalar.
+ *
+ * * @example
+ * ```ts
+ * const matrix = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ *
+ * const result1 = matrix.mulScalar(2);
+ * // => new Matrix2D(
+ * // [2, 4],
+ * // [6, 8],
+ * // [10, 12],
+ * // )
+ * ```
+ *
+ * @param s - The value by which to scale each term
+ */
+ public mulScalar(s: number): Matrix2D {
+ return new Matrix2D(
+ this.values[0] * s,
+ this.values[1] * s,
+ this.values[2] * s,
+ this.values[3] * s,
+ this.values[4] * s,
+ this.values[5] * s,
+ );
+ }
+
+ /**
+ * Translate the matrix by the dimensions of the provided vector.
+ *
+ * @remarks
+ * If `vec` is provided as a scalar, matrix will be translated uniformly
+ * by this factor.
+ *
+ * This method returns a new matrix representing the result of the
+ * computation. It will not modify the source matrix.
+ *
+ * @example
+ * ```ts
+ * const matrix = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ *
+ * const result1 = matrix.translate([2, 3]);
+ * // => new Matrix2D(
+ * // [1, 2],
+ * // [3, 4],
+ * // [16, 22],
+ * // )
+ *
+ * const result2 = matrix.translate(2);
+ * // => new Matrix2D(
+ * // [1, 2],
+ * // [3, 4],
+ * // [13, 18],
+ * // )
+ * ```
+ *
+ * @param vec - The vector by which to translate the matrix
+ */
+ public translate(vec: PossibleVector2): Matrix2D {
+ const v = new Vector2(vec);
+
+ return new Matrix2D(
+ this.values[0],
+ this.values[1],
+ this.values[2],
+ this.values[3],
+ this.values[0] * v.x + this.values[2] * v.y + this.values[4],
+ this.values[1] * v.x + this.values[3] * v.y + this.values[5],
+ );
+ }
+
+ /**
+ * Add the provided matrix to this matrix.
+ *
+ * @remarks
+ * This method returns a new matrix representing the result of the
+ * computation. It will not modify the source matrix.
+ *
+ * @example
+ * ```ts
+ * const a = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ * const a = new Matrix2D(
+ * [7, 8],
+ * [9, 10],
+ * [11, 12],
+ * );
+ *
+ * const result = a.add(b);
+ * // => Matrix2D(
+ * // [8, 10],
+ * // [12, 14],
+ * // [16, 18],
+ * // )
+ * ```
+ *
+ * @param other - The matrix to add
+ */
+ public add(other: Matrix2D): Matrix2D {
+ return new Matrix2D(
+ this.values[0] + other.values[0],
+ this.values[1] + other.values[1],
+ this.values[2] + other.values[2],
+ this.values[3] + other.values[3],
+ this.values[4] + other.values[4],
+ this.values[5] + other.values[5],
+ );
+ }
+
+ /**
+ * Subtract the provided matrix from this matrix.
+ *
+ * @remarks
+ * This method returns a new matrix representing the result of the
+ * computation. It will not modify the source matrix.
+ *
+ * @example
+ * ```ts
+ * const a = new Matrix2D(
+ * [1, 2],
+ * [3, 4],
+ * [5, 6],
+ * );
+ * const a = new Matrix2D(
+ * [7, 8],
+ * [9, 10],
+ * [11, 12],
+ * );
+ *
+ * const result = a.sub(b);
+ * // => Matrix2D(
+ * // [-6, -6],
+ * // [-6, -6],
+ * // [-6, -6],
+ * // )
+ * ```
+ *
+ * @param other - The matrix to subract
+ */
+ public sub(other: Matrix2D): Matrix2D {
+ return new Matrix2D(
+ this.values[0] - other.values[0],
+ this.values[1] - other.values[1],
+ this.values[2] - other.values[2],
+ this.values[3] - other.values[3],
+ this.values[4] - other.values[4],
+ this.values[5] - other.values[5],
+ );
+ }
+
+ public equals(other: Matrix2D, threshold: number = EPSILON): boolean {
+ return (
+ Math.abs(this.values[0] - other.values[0]) <= threshold &&
+ Math.abs(this.values[1] - other.values[1]) <= threshold &&
+ Math.abs(this.values[2] - other.values[2]) <= threshold &&
+ Math.abs(this.values[3] - other.values[3]) <= threshold &&
+ Math.abs(this.values[4] - other.values[4]) <= threshold &&
+ Math.abs(this.values[5] - other.values[5]) <= threshold
+ );
+ }
+
+ public exactlyEquals(other: Matrix2D): boolean {
+ return (
+ this.values[0] === other.values[0] &&
+ this.values[1] === other.values[1] &&
+ this.values[2] === other.values[2] &&
+ this.values[3] === other.values[3] &&
+ this.values[4] === other.values[4] &&
+ this.values[5] === other.values[5]
+ );
+ }
+}
diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts
index e02181bd..14df7058 100644
--- a/packages/core/src/types/index.ts
+++ b/packages/core/src/types/index.ts
@@ -7,6 +7,7 @@
export * from './Canvas';
export * from './Color';
export * from './Matrix';
+export * from './Matrix2D';
export * from './Origin';
export * from './Rect';
export * from './Spacing';