title: Diamond-square algorithm not working (rewrite code from JS to JAVA)
body: <p>I am trying rewrite code from <code>JS</code> to <code>Java</code>. The code is Diamond square algorithm.</p>
  
  <p>Source is: <a href="http://www.playfuljs.com/realistic-terrain-in-130-lines/" rel="nofollow noreferrer">http://www.playfuljs.com/realistic-terrain-in-130-lines/</a></p>
  
  <p>I rewrite code, but my code not working...</p>
  
  <p>Output is bad. </p>
  
  <p>My code in <code>Java</code> is:</p>
  
  <pre><code>public class MapGenerator {
  
      public static void main(String[] args) {
          MapGenerator mg = new MapGenerator(9);
          mg.generate();
          mg.printMap();
      }
  
      private int size, max;
      double[] map;
      int[][] matrix;
  
      public MapGenerator(int detail) {
          this.size = (int) Math.pow(2, detail) + 1;
          this.max = this.size - 1;
          this.map = new double[this.size * this.size];
      }
  
      private double get(int x, int y) {
          if (x &lt; 0 || x &gt; this.max || y &lt; 0 || y &gt; this.max) {
              return -1;
          }
          return this.map[x + this.size * y];
      }
  
      private void set(int x, int y, double val) {
          this.map[x + this.size * y] = val;
      }
  
      public void generate() {
          set(0, 0, max);
          set(this.max, 0, max / 2);
          set(this.max, this.max, 0);
          set(0, this.max, max / 2);
          divide(this.max);
  
          buildMatrix();
          saveTerrain(0, 0, 0, 0, matrix, "vystup.ter");
      }
  
      private void buildMatrix() {
          matrix = new int[size][size];
  
          for (int i = 0; i &lt; matrix.length; i++) {
              for (int j = 0; j &lt; matrix[0].length; j++) {
                  matrix[i][j] = (int) map[i + j];
              }
          }
  
      }
  
      private void divide(int size) {
          //? 
          double roughness = 0.7;
  
          int x, y, half = size / 2;
          double scale = roughness * size;
  
          if (half &lt; 1) {
              return;
          }
          for (y = half; y &lt; max; y += size) {
              for (x = half; x &lt; max; x += size) {
                  square(x, y, half, Library.randInt(0, 100) * scale * 2 - scale);
              }
          }
          for (y = 0; y &lt;= max; y += half) {
              for (x = (y + half) % size; x &lt;= max; x += size) {
                  diamond(x, y, half, Library.randInt(0, 100) * scale * 2 - scale);
              }
          }
          divide(size / 2);
      }
  
      private void square(int x, int y, int size, double offset) {
  
          double tmp_1 = get(x, y - size);   // top
          double tmp_2 = get(x + size, y);      // right
          double tmp_3 = get(x, y + size);     // bottom
          double tmp_4 = get(x - size, y);       // left
  
          set(x, y, ((tmp_1 + tmp_2 + tmp_3 + tmp_4) / 4.0) + offset);
      }
  
      private void diamond(int x, int y, int size, double offset) {
          double tmp_1 = get(x, y - size);      // top
          double tmp_2 = get(x + size, y);      // right
          double tmp_3 = get(x, y + size);      // bottom
          double tmp_4 = get(x - size, y);      // left
  
          set(x, y, ((tmp_1 + tmp_2 + tmp_3 + tmp_4) / 4.0) + offset);
      }
  
      public void printMap() {
          for (int i = 0; i &lt; matrix.length; i++) {
              for (int j = 0; j &lt; matrix[0].length; j++) {
                  System.out.print(matrix[i][j] + " ");
              }
              System.out.println("");
          }
      }
  
      public void saveTerrain(int canonX, int canonY, int targetX, int targetY,
              int[][] terrain, String fName) {
          int height = terrain.length;
          int width = terrain[0].length;
  
          DataOutputStream fout = null;
          try {
              // Samotný zápis dat
              fout = new DataOutputStream(new FileOutputStream(fName));
  
              fout.writeInt(width);
              fout.writeInt(height);
              fout.writeInt(canonX);
              fout.writeInt(canonY);
              fout.writeInt(targetX);
              fout.writeInt(targetY);
  
              for (int y = 0; y &lt; height; ++y) {
                  for (int x = 0; x &lt; width; ++x) {
                      fout.writeInt(terrain[y][x]);
                  }
              }
          } /*
           * Následuje pouze zavření souboru a ošetrení výjimek
           */ catch (FileNotFoundException e) {
              System.err.println("Nepovedlo se otevrit vystupni soubor.");
          } catch (IOException e) {
              System.err.println("Nepovedlo se zapsat vystupni soubor.");
          } finally {
              try {
                  if (fout != null) {
                      fout.close();
                  }
              } catch (IOException e) {
                  System.err.println("Nepovedlo se uzavrit vystupni soubor.");
              }
          }
      }
  }
  </code></pre>
  
  <p>Can anyone help me?</p>
  
  <p>Output of algo is <code>int[][]</code> which i visualise using another program to image..</p>
  
  <p>Visualise of output: </p>
  
  <p><img src="https://i.stack.imgur.com/xag9f.png" alt="OUTPUT IMAGE LINK"></p>
  
  <p>And this is how it should look </p>
  
  <p><img src="https://i.stack.imgur.com/po8b4.png" alt="OUTPUT IMAGE LINK"></p>
  
  <p><strong>I am not getting error, but the output is wrong.</strong>
  Thanks for help.</p>