In cellular automata, the Moore neighborhood is defined on a two-dimensional square lattice and is composed of a central cell and the eight cells that surround it. (From Wikipedia)

moore-neighborhood-with-cardinal-directions

Direction

  1. Find the coordinate in the left corner(NW)
  2. Iterate base on the coordinate above both vertically and horizontally 3 times

    • If the current x coordinate and y coordinate are the same as the center x, y coordinate.

      • Jump back to the next for loop immediately. (or keyword continue in this case)
    • Check if the coordinate is legal.

moore-neighborhood-illegal-example

▲ The center index is 1, red boxes are NOT legal, and green boxes are legal.

C++ Code

This demo code will return the min value of the Moore Neighborhood.

// check if the coordinate is legal
bool validCoord(int indexX, int indexY, int gridWidth, int gridHeight) {
    if (indexX < 0 || indexY < 0) {
        return false;
    }
    if (indexX >= gridWidth || indexY >= gridHeight) {
        return false;
    }
    return true;
}

int findMooreMin(int** grid, int gridWidth, int gridHeight,  int cIndexX, int cIndexY) {
    // Find the coordinate of NW location
    int nIndexX = cIndexX - 1;
    int nIndexY = cIndexY - 1;
    // Set the current index value as the min value so far
    int min = grid[nIndexX][nIndexY];
    // iterate 9 times total
    for (int y = nIndexY; y < nIndexY + 3; ++y) {
        for (int x = nIndexX; x < nIndexX + 3; ++x) {
            // check if the current index is the center index
            if (y == cIndexY && x == cIndexX) {
                continue;
            }
            // check if the coordinate is legal
            if (validCoord(x, y, gridWidth, gridHeight)) {
                // if the value located in the current index is smaller than min, update the min value
                if (grid[y][x] < min) {
                    min = grid[y][x];
                }
            }
        }
    }
    // return the min value
    return min;
}

Full Test Code:

#include <iostream>


int findMooreMin(int** grid, int gridWidth, int gridHeight,  int cIndexX, int cIndexY);

int main(int argc, char** argv) {
    // Create a new 3*3 array
    int** array = new int*[3];
    for (int i = 0; i < 3; ++i) {
        array[i] = new int[3];
    }

    // set all values to y*x
    for (int y = 0; y < 3; ++y) {
        for (int x = 0; x < 3; ++x) {
            array[y][x] = y*x;
        }
    }
    // set [y][x] to -10
    array[1][0] = -10;
    int min = findMooreMin(array, 3, 3, 1, 1);

    std::cout << min << std::endl;


    // Remove a 3*3 array
    for (int i = 0; i < 3; ++i) {
        delete[] array[i];
    }
    delete [] array;

    return 0;
}


// check if the coordinate is legal
bool validCoord(int indexX, int indexY, int gridWidth, int gridHeight) {
    if (indexX < 0 || indexY < 0) {
        return false;
    }
    if (indexX >= gridWidth || indexY >= gridHeight) {
        return false;
    }
    return true;
}

int findMooreMin(int** grid, int gridWidth, int gridHeight,  int cIndexX, int cIndexY) {
    // Find the coordinate of NW location
    int nIndexX = cIndexX - 1;
    int nIndexY = cIndexY - 1;
    // Set the current index value as the min value so far
    int min = grid[nIndexX][nIndexY];
    // iterate 9 times total
    for (int y = nIndexY; y < nIndexY + 3; ++y) {
        for (int x = nIndexX; x < nIndexX + 3; ++x) {
            // check if the current index is the center index
            if (y == cIndexY && x == cIndexX) {
                continue;
            }
            // check if the coordinate is legal
            if (validCoord(x, y, gridWidth, gridHeight)) {
                // if the value located in the current index is smaller than min, update the min value
                if (grid[y][x] < min) {
                    min = grid[y][x];
                }
            }
        }
    }
    // return the min value
    return min;
}

The return value is -10.

TOC