0

I have an algorithm that generates "continents" using simplex noise, so it'll generate a height map, and when a pixel/tile is above a certain level it'll be recognised as land, otherwise it's water.

The problem I have is sometimes land will surround a section that is below sea level. I would like to be able to identify those areas because they won't be sea, but rather land or a lake.

So the in following the 0's represent water and 1's represent land:

000000000
001110100
011111110
011001110
001101100
001111000
000000000

The 3 0's in the middle would be identified as non-sea.

I am familiar with the flood fill algorithm. So one way I could do it is to iterate through random points, use a flood fill if I find water, if it's over a certain size then it'll be the sea. Then once the sea has been recognised I can go through and identify non-sea bodies for water.

This seems inefficient as I will have to work with a larger area than what is loaded otherwise it'll falsely recognise areas as non-sea.

Is there a better way?

HedgepigMatt
  • 190
  • 3
  • 16
  • Your use of *water* and *sea* is confusing me. Having said that, you never mentioned what are those `1` and `0` representing in that bitmap – Alexander Dec 17 '12 at 18:05
  • Having to load adjacent blocks when checking the borders of the block doesn't really seem like an easily avoidable problem. – argentage Dec 17 '12 at 18:05
  • Alex I have clarified the 1's and 0's. Water covers the world and the largest body of water is the sea. – HedgepigMatt Dec 17 '12 at 18:34
  • airza you're probably right, it'd still be good to hear if there are any other solutions to this problem – HedgepigMatt Dec 17 '12 at 18:35
  • Could you generate continents with no lakes first and then randomly dig the lakes later? – irrelephant Dec 17 '12 at 18:48
  • I could investigate doing this, but the nature of the current algorithm is that "lakes" are created by chance, the system can't tell the difference between lakes and sea. – HedgepigMatt Dec 17 '12 at 19:12

2 Answers2

2

You mentioned you are already familiar with flood fill. Why not flood fill from the edge of the map to get the sea? Any body of water that doesn't floodfill from the edge would then be a lake.

Jimmy
  • 89,068
  • 17
  • 119
  • 137
  • Land may occur on the edge of the map. Sorry I would have provided a picture but it wouldn't allow me. – HedgepigMatt Dec 17 '12 at 18:30
  • @user1494801: But land must not occur on *every* edge pixel of the map, because if it did then there would be no sea -- any water would be part of one or more lakes! So find all non-land edge pixels and flood-fill from each one. – j_random_hacker Dec 17 '12 at 19:34
  • true but there is a chance a lake will be on the edge. The point I'm making is edge pixels are no more special than any other pixel/tile – HedgepigMatt Dec 17 '12 at 19:47
  • @user1494801: If water on the edge can be a lake, *how do you decide what the sea is?* :) Before anyone can come up with an algorithm to classify water as sea or lake, you need to come up with a crystal-clear *definition* of which is which. – j_random_hacker Dec 17 '12 at 22:24
2

You just want to compute the connected components of all of your 0 cells. The largest component is the sea, and any other components will be lakes. This can be done in linear time (see the Algorithms section of the linked page).

Keith Randall
  • 22,985
  • 2
  • 35
  • 54