code: private void setDirection( double pitValue, int row, int col, int[][] dir, double[] fact ) {
          dir[col][row] = 0; /* This necessary to repeat passes after level raised */
          double smax = 0.0;
  
          // examine adjacent cells first
          for( int k = 1; k <= 8; k++ ) {
              int cn = col + DIR_WITHFLOW_EXITING_INVERTED[k][0];
              int rn = row + DIR_WITHFLOW_EXITING_INVERTED[k][1];
              double pitN = pitIter.getSampleDouble(cn, rn, 0);
              if (isNovalue(pitN)) {
                  dir[col][row] = -1;
                  break;
              }
              if (dir[col][row] != -1) {
                  double slope = fact[k] * (pitValue - pitN);
                  if (slope > smax) {
                      smax = slope;
                      // maximum slope gives the drainage direction
                      dir[col][row] = k;
                  }
              }
          }
  
      }
docs: Calculate the drainage direction with the D8 method.
  
  <p>Find the direction that has the maximum
  slope and set it as the drainage direction the in the cell (r,c)
  in the dir matrix.
  
  @param pitValue the value of pit in row/col.
  @param row row of the cell in the matrix.
  @param col col of the cell in the matrix.
  @param dir the drainage direction matrix to set the dircetion in. The cell contains an int value in the range 0 to 8
  (or 10 if it is an outlet point).
  @param fact is the direction factor (1/lenght).