Search code examples
c#coordinatesgeneratorpoints

Dynamic Neighborhood Coordinates


For an image filter I want to generate a variable neighborhood. This is how my neighborhood looks like at the moment, it's a moore neighborhood.

private Point[] neighborhood = new Point[]
                                               {
                                                   new Point(-1,-1),
                                                   new Point(0,-1),
                                                   new Point(1,-1),

                                                   new Point(-1,0),
                                                   new Point(1,0),

                                                   new Point(-1,1), 
                                                   new Point(0,1), 
                                                   new Point(1,1), 
                                               };

When I want to change the size of the neighborhood this can become quite complicated. I want a funktion that returns all coordinates, like generateNeighborhood(8) would return this array of Points. Whats the best way to do this?


Solution

  • Something like this?

    private Point[] GetNeighbors(int count)
    {
        int a, x, y, c = count / 2;
        Point[] p = new Point[count * count];
    
        for (a = y = 0; y < count; y++)
            for (x = 0; x < count; x++)
                p[a++] = /* Create point here */
        return p;
    }
    

    I think you can add the missing piece of code ;)