c# - Check neighbouring numbers in matrix -


how count neighbouring ones in 3x3 matrix, this

1  1  1 1  0  0 0  0  0 

and desired output be( in square brackets indexes [x,y], care numbers, indexes accuracy) [0,0] - 2, [1,0]- 3, [2,0]- 1, [0,1]-2, etc... not count middle number in.

i have large matrix , task loop through each number, imagine matrix , count how many ones around number in middle. code, cant make work.

here method:

public int checkarea(int cordx, int cordy)     {         int count = 0;         (int = cordx - 1; <= cordx + 1; i++)         {             (int j = cordy - 1; j <= cordy + 1; j++)             {                 if (j != cordy && != cordx && tilefield[i, j])                 {                     count++;                 }             }         }        return count;           } 

it shouldnt index problem cos setup matrix this:

        tilefield = new boolean[width, height];         (int x = 0; x < width; x++)         {             (int y = 0; y < height; y++)             {                 tilefield[x, y] = false;             }         } 

but not print first , last column , row print method is:

public void print()     {         (int x = 1; x < this.width-1 ; x++)         {             (int y = 1; y < this.height -1; y++)             {                 if (!tilefield[x, y])                 {                     console.write("0 ");                 }                 else                 {                     console.write("1 ");                 }             }             console.writeline("");         }      } 

although might seem begs using loops reduce code, might want manually check each neighbor there 8 possibilities. solution below assumes aren't interested in "wrapping" around grid , there aren't additional rows/columns in field other coordinates want check (like buffer row , column top , left edge). i'd suggest doing following:

public int checkarea(int cordx, int cordy) {     int count = 0;     int fieldwidth = tilefield.getlength(1);     int fieldheight = tilefield.getlength(0);      // check neighbor north     if(cordy != 0)     {         count += getvalueatcoordinate(cordx,cordy - 1);     }     // check neighbor east     if (cordx < fieldwidth - 1)     {         count += getvalueatcoordinate(cordx + 1, cordy);          // ne neighbor         if(cordy != 0)         {             count += getvalueatcoordinate(cordx - 1, cordy - 1);         }          // se neighbor         if(cordy != fieldwidth - 1)         {             count += getvalueatcoordinate(cordx - 1, cordy + 1);         }     }     // check neighbor south     if (cordy < fieldheight - 1)     {         count += getvalueatcoordinate(cordx, cordy + 1);     }     // check neighbor west     if (cordx != 0)     {         count += getvalueatcoordinate(cordx - 1, cordy);          // nw neighbor         if(cordy != 0)         {             count += getvalueatcoordinate(cordx - 1, cordy - 1);         }          // sw neighbor         if(cordy != fieldheight - 1)         {             count += getvalueatcoordinate(cordx - 1, cordy + 1);         }     }      return count; }  public int getvalueatcoordinate(int x, int y) {     return tilefield[x,y] == true ? 1 : 0; } 

you use variety of other methods unless there specific reason don't want use simple path of checking each neighbor don't think save time loops. added method convert bool value @ given coordinate int of 1 true , 0 false appears matrix of type bools.

edit if treating 1-index array, change edge checks use fieldwidth , fieldheight variables not reduce 1.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -