42 lines
946 B
JavaScript
42 lines
946 B
JavaScript
function CellSet(definition) {
|
|
definition = definition || {};
|
|
this.cells = {};
|
|
if(definition.shape == 'rectangle') {
|
|
var xMax = definition.x + definition.width;
|
|
var yMax = definition.y + definition.height;
|
|
for(var i = definition.x; i < xMax; i++) {
|
|
for(var j = definition.y; j < yMax; j++) {
|
|
this.add(i, j);
|
|
}
|
|
}
|
|
} else if(definition.shape == 'points') {
|
|
for(var i = 0; i < definition.points.length; i++) {
|
|
this.cells[id(definition.points[i].i, definition.points[i].j)];
|
|
}
|
|
}
|
|
}
|
|
|
|
CellSet.prototype.add = function(i, j) {
|
|
this.cells[id(i, j)] = true;
|
|
}
|
|
|
|
CellSet.prototype.remove = function(i, j) {
|
|
delete this.cells[id(i, j)];
|
|
}
|
|
|
|
CellSet.prototype.contains = function(i, j) {
|
|
return !!this.cells[id(i, j)];
|
|
}
|
|
|
|
CellSet.prototype.isEmpty = function() {
|
|
return Object.keys(this.cells).length < 1;
|
|
}
|
|
|
|
return {
|
|
make: function(definition) {return new CellSet(definition);},
|
|
};
|
|
|
|
function id(i, j) {
|
|
return i + ':' + j;
|
|
}
|