xBLADEx - 3 months ago 17

C# Question

I have an 20x20 matrix with 0 values.

matrix[10,10] is chosen value: 1.

*full size image*

Distance is: 3.

How to do change neighbour values (distance: 3) using chebyshev distance algorithm in C#?

*full size image*

Algorithm can check, if chosen value 1 is not in center of matrix (for example: matrix[1,1] = 1) then will not out of matrix index range.

I read chebysev distance on Wikipedia, but I dont understand math formula.

Answer

If I understand you question correctly, you want to change all entries within a radius `r`

of some reference point `(x, y)`

to a defined value (where the radius is measured in the Chebyshev metric). You can do this by calculating the appropriate ranges on the axes:

```
var lowerX = Math.Max(0, x - radius);
var upperX = Math.Min(matrix.Columns - 1, x + radius);
var lowerY = Math.Max(0, y - radius);
var upperY = Math.Min(matrix.Rows - 1, y + radius);
for(int i = lowerX; i <= upperX; ++i)
for(int j = lowerY; j <= upperY; ++j)
matrix[i][j] = value;
```

The methods `matrix.Rows`

, `matrix.Columns`

, and the element access `matrix[][]`

are just used exemplarily. You have to use appropriate methods of your data structure.