From 88800ad83d062c2ce45dd9ec50a0f50a5566e9ad Mon Sep 17 00:00:00 2001 From: Pranava Pai N Date: Sun, 20 Sep 2026 19:57:44 +0530 Subject: [PATCH 1/2] Fix: Added fixes for custom matrix passing with NXN and 3X3 transpose with regression tests --- src/math/Matrices/Matrix.js | 32 ++++++++++++++++---------------- test/unit/math/p5.Matrix.js | 6 ++---- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/math/Matrices/Matrix.js b/src/math/Matrices/Matrix.js index 91d3ecc263..7bae91e7b7 100644 --- a/src/math/Matrices/Matrix.js +++ b/src/math/Matrices/Matrix.js @@ -1774,9 +1774,10 @@ export class Matrix extends MatrixInterface { */ #transposeNxN() { const n = this.#sqDimention; + const source = this.matrix.slice(); for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { - this.matrix[i * n + j] = this.matrix[j * n + i]; + this.matrix[i * n + j] = source[j * n + i]; } } return this; @@ -1790,7 +1791,6 @@ export class Matrix extends MatrixInterface { * @chainable */ #transpose4x4(a) { - console.log('====> 4x4'); let a01, a02, a03, a12, a13, a23; if (a instanceof Matrix) { a01 = a.matrix[1]; @@ -1856,21 +1856,21 @@ export class Matrix extends MatrixInterface { * @chainable */ #transpose3x3(mat3) { - if (mat3 === undefined) { - mat3 = this.mat3; + const source = mat3 instanceof Matrix ? mat3.mat3 : mat3 ?? this.mat3; + if (!source) { + return this; } - const a01 = mat3[1]; - const a02 = mat3[2]; - const a12 = mat3[5]; - this.mat3[0] = mat3[0]; - this.mat3[1] = mat3[3]; - this.mat3[2] = mat3[6]; - this.mat3[3] = a01; - this.mat3[4] = mat3[4]; - this.mat3[5] = mat3[7]; - this.mat3[6] = a02; - this.mat3[7] = a12; - this.mat3[8] = mat3[8]; + + const sourceCopy = Array.from(source); + this.matrix[0] = sourceCopy[0]; + this.matrix[1] = sourceCopy[3]; + this.matrix[2] = sourceCopy[6]; + this.matrix[3] = sourceCopy[1]; + this.matrix[4] = sourceCopy[4]; + this.matrix[5] = sourceCopy[7]; + this.matrix[6] = sourceCopy[2]; + this.matrix[7] = sourceCopy[5]; + this.matrix[8] = sourceCopy[8]; return this; } diff --git a/test/unit/math/p5.Matrix.js b/test/unit/math/p5.Matrix.js index 762d7f3779..ce42954616 100644 --- a/test/unit/math/p5.Matrix.js +++ b/test/unit/math/p5.Matrix.js @@ -406,15 +406,13 @@ suite('p5.Matrix', function () { ]); }); - // TODO: matrix transpose This needs to be added to the legacy tests - it.skip('should transpose a 3x3 matrix correctly', () => { + it('should transpose a 3x3 matrix correctly', () => { const mat = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]); mat.transpose(mat); expect(mat.mat3).toEqual([1, 4, 7, 2, 5, 8, 3, 6, 9]); }); - // TODO: matrix transpose This needs to be added to the legacy tests - it.skip('should transpose a 3x3 matrix from an array correctly', () => { + it('should transpose a 3x3 matrix from an array correctly', () => { const mat = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]); mat.transpose([1, 2, 3, 4, 5, 6, 7, 8, 9]); From 3ed9f629476ac3679e1dd75c40fd6b09340fcd0c Mon Sep 17 00:00:00 2001 From: Pranava Pai N Date: Mon, 21 Sep 2026 09:29:54 +0530 Subject: [PATCH 2/2] Fix: 3X3 Transpose with custom matrix with regression tests added --- src/math/Matrices/Matrix.js | 33 ++++++++++++++++++--------------- test/unit/math/p5.Matrix.js | 4 ++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/math/Matrices/Matrix.js b/src/math/Matrices/Matrix.js index 7bae91e7b7..e7d9748904 100644 --- a/src/math/Matrices/Matrix.js +++ b/src/math/Matrices/Matrix.js @@ -1774,10 +1774,9 @@ export class Matrix extends MatrixInterface { */ #transposeNxN() { const n = this.#sqDimention; - const source = this.matrix.slice(); for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { - this.matrix[i * n + j] = source[j * n + i]; + this.matrix[i * n + j] = this.matrix[j * n + i]; } } return this; @@ -1791,6 +1790,7 @@ export class Matrix extends MatrixInterface { * @chainable */ #transpose4x4(a) { + console.log('====> 4x4'); let a01, a02, a03, a12, a13, a23; if (a instanceof Matrix) { a01 = a.matrix[1]; @@ -1856,21 +1856,24 @@ export class Matrix extends MatrixInterface { * @chainable */ #transpose3x3(mat3) { - const source = mat3 instanceof Matrix ? mat3.mat3 : mat3 ?? this.mat3; - if (!source) { - return this; + if (mat3 instanceof Matrix) { + mat3 = mat3.mat3; + } else if (mat3 === undefined) { + mat3 = this.mat3; } - const sourceCopy = Array.from(source); - this.matrix[0] = sourceCopy[0]; - this.matrix[1] = sourceCopy[3]; - this.matrix[2] = sourceCopy[6]; - this.matrix[3] = sourceCopy[1]; - this.matrix[4] = sourceCopy[4]; - this.matrix[5] = sourceCopy[7]; - this.matrix[6] = sourceCopy[2]; - this.matrix[7] = sourceCopy[5]; - this.matrix[8] = sourceCopy[8]; + const a01 = mat3[1]; + const a02 = mat3[2]; + const a12 = mat3[5]; + this.mat3[0] = mat3[0]; + this.mat3[1] = mat3[3]; + this.mat3[2] = mat3[6]; + this.mat3[3] = a01; + this.mat3[4] = mat3[4]; + this.mat3[5] = mat3[7]; + this.mat3[6] = a02; + this.mat3[7] = a12; + this.mat3[8] = mat3[8]; return this; } diff --git a/test/unit/math/p5.Matrix.js b/test/unit/math/p5.Matrix.js index ce42954616..7e0f03ca67 100644 --- a/test/unit/math/p5.Matrix.js +++ b/test/unit/math/p5.Matrix.js @@ -409,7 +409,7 @@ suite('p5.Matrix', function () { it('should transpose a 3x3 matrix correctly', () => { const mat = new p5.Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9]); mat.transpose(mat); - expect(mat.mat3).toEqual([1, 4, 7, 2, 5, 8, 3, 6, 9]); + expect(toArray(mat.mat3)).toEqual([1, 4, 7, 2, 5, 8, 3, 6, 9]); }); it('should transpose a 3x3 matrix from an array correctly', () => { @@ -417,7 +417,7 @@ suite('p5.Matrix', function () { mat.transpose([1, 2, 3, 4, 5, 6, 7, 8, 9]); - expect(mat.mat3).toEqual([1, 4, 7, 2, 5, 8, 3, 6, 9]); + expect(toArray(mat.mat3)).toEqual([1, 4, 7, 2, 5, 8, 3, 6, 9]); }); }); describe.skip('Determinant', () => {