Constellations/js/Grid/Util.js

47 lines
868 B
JavaScript
Raw Normal View History

2022-08-10 21:12:39 +02:00
import * as Vector from Geometry.Vector;
return {
2022-08-10 21:12:39 +02:00
at: at,
column: column,
generate: generate,
iter: iter,
2022-08-10 21:12:39 +02:00
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++) {
2022-08-10 21:12:39 +02:00
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);
}
2022-08-10 21:12:39 +02:00
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];});
}