Skip to content

Instantly share code, notes, and snippets.

@vyuvalv
Last active November 22, 2020 15:26
Show Gist options
  • Save vyuvalv/fe649ab21c010bc457c0916aa99c629c to your computer and use it in GitHub Desktop.
Save vyuvalv/fe649ab21c010bc457c0916aa99c629c to your computer and use it in GitHub Desktop.
Handling Edge and Corners - getNeighbourhoodCells
// Will handle all Corners, Edges and Other Cells in Grid to find the neighbours of the the current examined cell
getNeighbourhoodCells(col, row, lastColumn, lastRow) {
const indexStart = -1, indexEnd = 2;
// TOP LEFT CORNER - 3 neighbours
if (col === 0 && row === 0) {
return {
xStart: col,
xEnd: indexEnd, // 2
yStart: row,
yEnd: indexEnd // 2
}
}
// BOTTOM LEFT CORNER - 3 neighbours
else if (col === 0 && row === lastRow) {
return {
xStart : col, // 0
xEnd : indexEnd, // 2
yStart : indexStart, // -1
yEnd : indexEnd + indexStart // 1
}
}
// TOP RIGHT CORNER - 3 neighbours
else if (row === 0 && col === lastColumn) {
return {
xStart : indexStart, // -1;
xEnd : indexEnd + indexStart, // 1
yStart : row, // 0,
yEnd : indexEnd, // 2
}
}
// BOTTOM RIGHT CORNER - 3 neighbours
else if (col === lastColumn && row === lastRow) {
return {
xStart: indexStart, // -1;
xEnd: indexEnd + indexStart, // 1
yStart: indexStart,// -1;
yEnd: indexEnd + indexStart // 1
}
}
// TOP ROW EDGE - 5 neighbours
else if (row === 0 && col > 0 && col < lastColumn) {
return {
xStart: indexStart, // -1,
xEnd :indexEnd, // 2,
yStart : row, // 0,
yEnd : indexEnd // 2
}
}
// BOTTOM ROW EDGE - 5 neighbours
else if (row === lastRow && col > 0 && col < lastColumn) {
return {
xStart: indexStart, // -1,
xEnd: indexEnd, // 2,
yStart: indexStart, // -1,
yEnd: indexEnd + indexStart // 1,
}
}
// RIGHT COLUMN EDGE - 5 neighbours
else if (col === lastColumn && row > 0 && row < lastRow) {
return {
xStart : indexStart, // -1,
xEnd : indexEnd + indexStart, // 1,
yStart : indexStart, // -1,
yEnd : indexEnd // 2,
}
}
// LEFT COLUMN EDGE - 5 neighbours
else if (col === 0 && row > 0 && row < lastRow) {
return {
xStart : col, // 0,
xEnd : indexEnd,// 2,
yStart : indexStart, // -1,
yEnd : indexEnd //2,
}
}
// ALL OTHERS CELLS - 8 neighbours
else {
return {
xStart : indexStart, // -1,
xEnd : indexEnd, // 2,
yStart :indexStart, // -1,
yEnd : indexEnd // 2,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment