# Diamond square algorithm

I'm working on generating terrain on already generated landmass. The landmass and ocean around it consist of hexagons. Hexagons aren't really a concern for me, because I can always interpolate the values algorithms generates with the vertices on the grid. As you can see on the image below, blue is ocean, brown is land.

Problem is, I need the shore to have 0 height, can it even be done with diamond square algorithm?

• By "the shore" you mean only the hexagons directly touching the sea? That's quite relevant for the problem at hand. Because if your answer to that is a yes, then my suggestion would be: why not only detect the land hexagons that touch the the blue ones?
– MAnd
Nov 27, 2015 at 21:44
• What's a diamond square algorithm? Do you mean a hex grid? Where is their height defined?
– Anko
Nov 27, 2015 at 23:36
• Shore are indeed the brown hexagons that are touching the blue ones. I can define height of every such vertex as 0. Question is, how do I generate heightmap with some variation of diamon square algorithm for the rest of the vertices? Nov 28, 2015 at 13:28
• I'm sure one can make a modified version of diamond-square that works for hexagonal instead of square tiles, but that seems irrelevant to this question as you have noticed yourself ("[not] really a concern for me"). The problem seems to be you want to use a heightmap generation algorithm while predefining the height for specific vertices. I have to ask: Why the requirement of needing to work on a predefined landmass? Why can't you derive land and ocean cells after heightmap generation?
– Eric
Nov 30, 2015 at 16:37
• @Anko Wikipedia has a decent overview of the Diamond-square algorithm. Nov 30, 2015 at 17:30

Diamond square algorithm works with random height add-on.

One thing you could do is to make a map that will give limits to these random numbers for each part of the map.

This map will have very low to null values in the sea region and on the shore. This map will allow higher values in other parts of the map.

You will then preserve the random generation and have realistic shores.

If you let low values in the sea side, you'll even be able to generate little random private islands ;)

Diamond Square doesn't work well with hexagons, but you did say you have a way around that with interpolation. You can seed the shore line with 0 and default the other heights to something else. During the process, don't generate the heights that are already 0.

Would that work?

• As far as I understand how the algorithm works, it can cause problems. The algorithm starts with 4 seeded values at corners. I can ofcourse seed some points inside, but when I reach those points during one of algorithm's steps, the calculated value there may differ vastly, causing weird peaks. I may be wrong though, hope I am :) Nov 29, 2015 at 8:07