How to implement the diamond-square algorithm?
-
10-03-2021 - |
Question
I was reading this: http://www.gameprogrammer.com/fractal.html#diamond
And it says:
This is the starting-point for the iterative subdivision routine, which is in two steps:
The diamond step: Taking a square of four points, generate a random value at the square midpoint, where the two diagonals meet. The midpoint value is calculated by averaging the four corner values, plus a random amount. This gives you diamonds when you have multiple squares arranged in a grid.
The square step: Taking each diamond of four points, generate a random value at the center of the diamond. Calculate the midpoint value by averaging the corner values, plus a random amount generated in the same range as used for the diamond step. This gives you squares again.
I don't understand this. How does taking the midpoint of every square make a diamond? How does taking the midpoint of every diamond make a square?
Can someone provide language-agnostic code for how to do this?
Edit:
Step 1: you have a grid and make the four corners uniform height:
* ┬ ┬ ┬ *
├ ┼ ┼ ┼ ┤
├ ┼ ┼ ┼ ┤
├ ┼ ┼ ┼ ┤
* ┴ ┴ ┴ *
Step 2: you take the midpoint of the square and set it to the average of all 4 corners plus a random value:
* ┬ ┬ ┬ *
├ ┼ ┼ ┼ ┤
├ ┼ * ┼ ┤
├ ┼ ┼ ┼ ┤
* ┴ ┴ ┴ *
Now what? I don't see a diamond anywhere
Solution
Put two of the squares together: "this gives you diamonds when you have multiple squares in a grid." Same for above/below the square.
The squaring step makes a new set of squares, offset from the first, with the vertices defined by the diamond's midpoints.