Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Trigonometry solution solution in Clear category for [old] Moore Neighbourhood by movieclip
"use strict";
function countNeighbours(data, row, col)
{
var counter = 0;
for (var i = 0; i < 2 * Math.PI; i += Math.PI / 4)
{
counter += getCell(row + Math.round(Math.sin(i)), col + Math.round(Math.cos(i)));
}
function getCell(r,c){return r >= 0 && c >= 0 && r < data.length && c < data[r].length ? data[r][c] : 0;};
return counter;
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(countNeighbours([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0]], 1, 2), 3, "1st example");
assert.equal(countNeighbours([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0]], 0, 0), 1, "2nd example");
assert.equal(countNeighbours([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], 0, 2), 3, "Dense corner");
assert.equal(countNeighbours([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]], 1, 1), 0, "Single");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Oct. 5, 2016
Comments: