/**
  * Create "diamonds (4 per each square)" structure by splitting the squares in the middle and
  * creating 4 diamond shapes. This is done by setting a value to the middle of each square.
  *
  * @param iteration The iteration number.
  * @param randRange The current random range.
  * @param random The random.
  * @param modifier The height modifier.
  */
 private void diamond(int iteration, float randRange, Random random, HeightModifier modifier) {
   final int stepSize = mPatchRowsCols >> iteration;
   final int stepSizeH = stepSize / 2;
   for (int z = 0; z < mPatchRowsCols; z += stepSize) {
     for (int x = 0; x < mPatchRowsCols; x += stepSize) {
       final float br = modifier.get(x, z);
       final float bl = modifier.get(x + stepSize, z);
       final float tr = modifier.get(x, z + stepSize);
       final float tl = modifier.get(x + stepSize, z + stepSize);
       final float value = getRandomValue(randRange, random) + (br + bl + tr + tl) / 4;
       modifier.set(x + stepSizeH, z + stepSizeH, value);
     }
   }
 }
  /**
   * Create a "square" structure out of each diamond. This is done by adding a new value in the
   * middle of the long-side of each diamond.
   *
   * @param iteration The iteration number.
   * @param randRange The current random range.
   * @param random The random.
   * @param modifier The height modifier.
   */
  private void square(int iteration, float randRange, Random random, HeightModifier modifier) {
    final int stepSize = mPatchRowsCols >> iteration;
    final int stepSizeH = stepSize / 2;
    boolean toggle = true;
    for (int z = 0; z < mPatchSideVertices; z += stepSizeH) {
      final boolean hasTop = z < (mPatchSideVertices - 1);
      final boolean hasBottom = z > 0;
      int x = toggle ? stepSizeH : 0;
      toggle = !toggle;
      for (; x < mPatchSideVertices; x += stepSize) {
        if (modifier.isModifiable(x, z)) {
          final boolean hasRight = x > 0;
          final boolean hasLeft = x < (mPatchSideVertices - 1);

          int count = 0;
          float value = 0;
          if (hasRight) {
            count++;
            value += modifier.get(x - stepSizeH, z);
          }
          if (hasLeft) {
            count++;
            value += modifier.get(x + stepSizeH, z);
          }
          if (hasBottom) {
            count++;
            value += modifier.get(x, z - stepSizeH);
          }
          if (hasTop) {
            count++;
            value += modifier.get(x, z + stepSizeH);
          }
          modifier.set(x, z, (value / count) + getRandomValue(randRange, random));
        } else {
          modifier.copyUnModifiable(x, z);
        }
      }
    }
  }