Add periodic, neighborhood options; fix #22, fix #23, fix #24

This commit is contained in:
Riley Shaw 2014-11-04 04:48:30 -05:00
parent 080547d05b
commit 86440d10bf
5 changed files with 213 additions and 39 deletions

View File

@ -123,6 +123,7 @@ var factory = (function () {
} else return this.energy !== this.maxEnergy;
};
baseCA.prototype.actionRadius = 1;
baseCA.prototype.boundEnergy = function () {};
baseCA.prototype.isDead = function () { return false; };
baseCA.prototype.process = function (neighbors, x, y) {};
@ -154,7 +155,7 @@ var factory = (function () {
};
}
var color = options.color;
var color = options.color || options.colour;
// set the color randomly if none is provided
if (typeof color !== 'object' || color.length !== 3) {
options.color = [_.random(255), _.random(255), _.random(255)];
@ -185,12 +186,14 @@ var factory = (function () {
function () { init.call(this); } :
function () {};
var color = options.color;
var color = options.color = options.color || options.colour;
// set the color randomly if none is provided
if (typeof color !== 'object' || color.length !== 3) {
options.color = [_.random(255), _.random(255), _.random(255)];
}
options.colorFn = options.colorFn || options.colourFn;
types[type].prototype = new baseCA();
types[type].prototype.constructor = types[type];

View File

@ -16,9 +16,13 @@ var dom = require('./dom.js');
* "background" option is required if trails is set
* @param {array} background an RGB triplet for the canvas' background
*/
function Terrarium(width, height, options) {
function Terrarium (width, height, options) {
var cellSize, neighborhood;
options = options || {};
var cellSize = options.cellSize || 10;
cellSize = options.cellSize || 10;
neighborhood = options.neighborhood || options.neighbourhood;
if (typeof neighborhood === 'string') neighborhood = neighborhood.toLowerCase();
this.width = width;
this.height = height;
this.cellSize = cellSize;
@ -28,6 +32,7 @@ function Terrarium(width, height, options) {
this.grid = [];
this.nextFrame = false;
this.hasChanged = false;
this.getNeighborCoords = _.getNeighborCoordsFn(width, height, neighborhood === 'vonneumann', options.periodic);
}
/**
@ -110,7 +115,7 @@ Terrarium.prototype.step = function (steps) {
function processCreaturesInner (creature, x, y) {
if (creature) {
var neighbors = _.map(
_.getNeighborCoords(x, y, gridWidth - 1, gridHeight - 1, creature.actionRadius),
self.getNeighborCoords(x, y, creature.actionRadius),
zipCoordsWithNeighbors
);
var result = creature.process(neighbors, x, y);

View File

@ -9,26 +9,105 @@ var _ = require('../lodash_custom/lodash.custom.min.js')._;
* Takes a cell and returns the coordinates of its neighbors
* @param {int} x0 - x position of cell
* @param {int} y0 - y position of cell
* @param {int} xMax - maximum x index i.e. grid width - 1
* @param {int} yMax - maximum x index i.e. grid height - 1
* @param {int} xMax - maximum x index i.e. grid width
* @param {int} yMax - maximum x index i.e. grid height
* @param {int} radius - (default = 1) neighbor radius
* @return {array} - an array of [x, y] pairs of the neighboring cells
*/
_.getNeighborCoords = function (x0, y0, xMax, yMax, radius) {
var coords = [], current, xLo, xHi, yLo, yHi;
if (typeof radius !== 'number' || radius < 1) radius = 1;
_.getNeighborCoordsFn = function (xMax, yMax, vonNeumann, periodic) {
if (periodic) {
if (vonNeumann) {
// periodic von neumann
return function (x0, y0, radius) {
var coords = [], x, rX, y, rY, rYMax;
xLo = Math.max(0, x0 - radius);
yLo = Math.max(0, y0 - radius);
xHi = Math.min(x0 + radius, xMax);
yHi = Math.min(y0 + radius, yMax);
for (rX = -radius; rX <= radius; ++rX) {
rYMax = radius - Math.abs(rX);
for (rY = -rYMax; rY <= rYMax; ++rY) {
x = ((rX + x0) % xMax + xMax) % xMax;
y = ((rY + y0) % yMax + yMax) % yMax;
if (x !== x0 || y !== y0) {
coords.push({
x: x,
y: y
});
}
}
}
for (var x = xLo; x <= xHi; x++)
for (var y = yLo; y <= yHi; y++)
if (x !== x0 || y !== y0)
coords.push({ x: x, y: y });
return coords;
};
}
else {
// periodic moore
return function (x0, y0, radius) {
var coords = [], x, xLo, xHi, y, yLo, yHi;
return coords;
xLo = x0 - radius;
yLo = y0 - radius;
xHi = x0 + radius;
yHi = y0 + radius;
for (x = xLo; x <= xHi; ++x) {
for (y = yLo; y <= yHi; ++y) {
if (x !== x0 || y !== y0) {
coords.push({
x: (x % xMax + xMax) % xMax,
y: (y % yMax + yMax) % yMax
});
}
}
}
return coords;
};
}
} else {
// non-periodic, need to restrict to within [0, max)
xMax -= 1;
yMax -= 1;
if (vonNeumann) {
//non-periodic von-neumann
return function (x0, y0, radius) {
var coords = [], x, rX, y, rY, rYMax;
for (rX = -radius; rX <= radius; ++rX) {
rYMax = radius - Math.abs(rX);
for (rY = -rYMax; rY <= rYMax; ++rY) {
x = rX + x0;
y = rY + y0;
if (x >= 0 && y >=0 && x <= xMax && y <= yMax && (x !== x0 || y !== y0)) {
coords.push({
x: x,
y: y
});
}
}
}
return coords;
};
}
else {
// non-periodic moore
return function (x0, y0, radius) {
var coords = [], x, xLo, xHi, y, yLo, yHi;
xLo = Math.max(0, x0 - radius);
yLo = Math.max(0, y0 - radius);
xHi = Math.min(x0 + radius, xMax);
yHi = Math.min(y0 + radius, yMax);
for (x = xLo; x <= xHi; ++x)
for (y = yLo; y <= yHi; ++y)
if (x !== x0 || y !== y0)
coords.push({ x: x, y: y });
return coords;
};
}
}
};
_.pickRandomWeighted = function (weightedArrays) {

125
dist/terra.js vendored
View File

@ -134,6 +134,7 @@ var factory = (function () {
} else return this.energy !== this.maxEnergy;
};
baseCA.prototype.actionRadius = 1;
baseCA.prototype.boundEnergy = function () {};
baseCA.prototype.isDead = function () { return false; };
baseCA.prototype.process = function (neighbors, x, y) {};
@ -165,7 +166,7 @@ var factory = (function () {
};
}
var color = options.color;
var color = options.color || options.colour;
// set the color randomly if none is provided
if (typeof color !== 'object' || color.length !== 3) {
options.color = [_.random(255), _.random(255), _.random(255)];
@ -196,12 +197,14 @@ var factory = (function () {
function () { init.call(this); } :
function () {};
var color = options.color;
var color = options.color = options.color || options.colour;
// set the color randomly if none is provided
if (typeof color !== 'object' || color.length !== 3) {
options.color = [_.random(255), _.random(255), _.random(255)];
}
options.colorFn = options.colorFn || options.colourFn;
types[type].prototype = new baseCA();
types[type].prototype.constructor = types[type];
@ -317,9 +320,13 @@ var dom = require('./dom.js');
* "background" option is required if trails is set
* @param {array} background an RGB triplet for the canvas' background
*/
function Terrarium(width, height, options) {
function Terrarium (width, height, options) {
var cellSize, neighborhood;
options = options || {};
var cellSize = options.cellSize || 10;
cellSize = options.cellSize || 10;
neighborhood = options.neighborhood || options.neighbourhood;
if (typeof neighborhood === 'string') neighborhood = neighborhood.toLowerCase();
this.width = width;
this.height = height;
this.cellSize = cellSize;
@ -329,6 +336,7 @@ function Terrarium(width, height, options) {
this.grid = [];
this.nextFrame = false;
this.hasChanged = false;
this.getNeighborCoords = _.getNeighborCoordsFn(width, height, neighborhood === 'vonneumann', options.periodic);
}
/**
@ -411,7 +419,7 @@ Terrarium.prototype.step = function (steps) {
function processCreaturesInner (creature, x, y) {
if (creature) {
var neighbors = _.map(
_.getNeighborCoords(x, y, gridWidth - 1, gridHeight - 1, creature.actionRadius),
self.getNeighborCoords(x, y, creature.actionRadius),
zipCoordsWithNeighbors
);
var result = creature.process(neighbors, x, y);
@ -553,26 +561,105 @@ var _ = require('../lodash_custom/lodash.custom.min.js')._;
* Takes a cell and returns the coordinates of its neighbors
* @param {int} x0 - x position of cell
* @param {int} y0 - y position of cell
* @param {int} xMax - maximum x index i.e. grid width - 1
* @param {int} yMax - maximum x index i.e. grid height - 1
* @param {int} xMax - maximum x index i.e. grid width
* @param {int} yMax - maximum x index i.e. grid height
* @param {int} radius - (default = 1) neighbor radius
* @return {array} - an array of [x, y] pairs of the neighboring cells
*/
_.getNeighborCoords = function (x0, y0, xMax, yMax, radius) {
var coords = [], current, xLo, xHi, yLo, yHi;
if (typeof radius !== 'number' || radius < 1) radius = 1;
_.getNeighborCoordsFn = function (xMax, yMax, vonNeumann, periodic) {
if (periodic) {
if (vonNeumann) {
// periodic von neumann
return function (x0, y0, radius) {
var coords = [], x, rX, y, rY, rYMax;
xLo = Math.max(0, x0 - radius);
yLo = Math.max(0, y0 - radius);
xHi = Math.min(x0 + radius, xMax);
yHi = Math.min(y0 + radius, yMax);
for (rX = -radius; rX <= radius; ++rX) {
rYMax = radius - Math.abs(rX);
for (rY = -rYMax; rY <= rYMax; ++rY) {
x = ((rX + x0) % xMax + xMax) % xMax;
y = ((rY + y0) % yMax + yMax) % yMax;
if (x !== x0 || y !== y0) {
coords.push({
x: x,
y: y
});
}
}
}
for (var x = xLo; x <= xHi; x++)
for (var y = yLo; y <= yHi; y++)
if (x !== x0 || y !== y0)
coords.push({ x: x, y: y });
return coords;
};
}
else {
// periodic moore
return function (x0, y0, radius) {
var coords = [], x, xLo, xHi, y, yLo, yHi;
return coords;
xLo = x0 - radius;
yLo = y0 - radius;
xHi = x0 + radius;
yHi = y0 + radius;
for (x = xLo; x <= xHi; ++x) {
for (y = yLo; y <= yHi; ++y) {
if (x !== x0 || y !== y0) {
coords.push({
x: (x % xMax + xMax) % xMax,
y: (y % yMax + yMax) % yMax
});
}
}
}
return coords;
};
}
} else {
// non-periodic, need to restrict to within [0, max)
xMax -= 1;
yMax -= 1;
if (vonNeumann) {
//non-periodic von-neumann
return function (x0, y0, radius) {
var coords = [], x, rX, y, rY, rYMax;
for (rX = -radius; rX <= radius; ++rX) {
rYMax = radius - Math.abs(rX);
for (rY = -rYMax; rY <= rYMax; ++rY) {
x = rX + x0;
y = rY + y0;
if (x >= 0 && y >=0 && x <= xMax && y <= yMax && (x !== x0 || y !== y0)) {
coords.push({
x: x,
y: y
});
}
}
}
return coords;
};
}
else {
// non-periodic moore
return function (x0, y0, radius) {
var coords = [], x, xLo, xHi, y, yLo, yHi;
xLo = Math.max(0, x0 - radius);
yLo = Math.max(0, y0 - radius);
xHi = Math.min(x0 + radius, xMax);
yHi = Math.min(y0 + radius, yMax);
for (x = xLo; x <= xHi; ++x)
for (y = yLo; y <= yHi; ++y)
if (x !== x0 || y !== y0)
coords.push({ x: x, y: y });
return coords;
};
}
}
};
_.pickRandomWeighted = function (weightedArrays) {

2
dist/terra.min.js vendored

File diff suppressed because one or more lines are too long