Constellations/js/Grid/Util.js
2022-08-11 09:58:57 +02:00

46 lines
868 B
JavaScript

import * as Vector from Geometry.Vector;
return {
at: at,
column: column,
generate: generate,
iter: iter,
row: row,
set: set,
square: square
};
function generate(width, height, f) {
var result = [];
for(var row = 0; row < height; row++) {
result[row] = [];;
for(var column = 0; column < width; column++) {
result[row].push(f(Vector.make(row, column)));
}
}
return result;
}
function iter(grid, f) {
generate(grid.length > 0 ? grid[0].length : null, grid.length, f);
}
function square(size, value) {
return generate(size, size, function() {return value;});
}
function at(grid, vector) {
return grid[vector.row][vector.column];
}
function set(grid, vector, value) {
return grid[vector.row][vector.column] = value;
}
function row(grid, n) {
return grid[n];
}
function column(grid, n) {
return grid.map(function(row) {return row[n];});
}