My openprocessing.org JavaScript code listed below produces the output I want but doesn’t seem very elegant to me especially the “.M” in “matrices[mat].M[row][col]”. Any comments to improve the quality of my code would be appreciated.
The purpose of this code snippet is so I can learn how to appropriately deal with a list of matrices. Eventually I hope to write a program that computes 1344 5x5 Latin squares and store them all in one variable.
The following correct output is what the program produces:
let matrices = [];
function setup() {
createCanvas(windowWidth, windowHeight);
matrices.push(new Matrix([ [0,1,2], [1,2,3], [2,3,4] ]));
matrices.push(new Matrix([ [1,2,3], [2,3,4], [3,4,5] ]));
matrices.push(new Matrix([ [2,3,4], [3,4,5], [4,5,6] ]));
}
function draw() {
background('black');
fill('white');
sum=0;
for( mat=0; mat<matrices.length; mat++ ) {
for( row=0; row<3; row++ ) {
for( col=0; col<3; col++ ) {
sum += matrices[mat].M[row][col];
textSize(20);
text( matrices[mat].M[row][col], 500+20*col, 100+120*mat+30*row);
}
}
}
textSize(30);
text( 'Sum=', 500, 500 );
text( sum, 500, 540 );
}
class Matrix {
constructor(m) {
this.M = m;
}
}
