How to do it...

We will implement the Convolve function:

  1. Declare the Convolve function:
public static void Convolve( 
        float[,] matrix, 
        ref float[,] source, 
        ref float[,] destination) 
{ 
    // next steps 
}
  1. Initialize the variables for handling the computations and the traversal of arrays:
int matrixLength = matrix.GetLength(0); 
int size = (int)(matrixLength - 1) / 2; 
int height = source.GetLength(0); 
int width = source.GetLength(1); 
int I, j, k, m; 
  1. Create the first loop for iterating over the destination and source grids:
for (i = 0; i < width-- size; i++) 
{ 
    for (j = 0; j < height-- size; j++) 
    { 
        // next steps 
    } 
} 
  1. Implement the second loop for iterating over the filter matrix:
destination[i, j] = 0f; for (k = 0; k < matrixLength; k++) ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.