# Algorithm to generate water flow map, given a terrain

I've posted the same question at GameDev Stack Exchange, but unfortunately I am not getting any response. So I am going to post ( and reword) it here. Hopefully I can get an answer!

I have a terrain ( in the form of mesh/element), and I want to indicate how the water flows in the terrain, subjected to gravity force and the terrain condition ( of course).

A crude way of doing this is to take every single mesh element, then draw the water flow direction from the highest point to the lowest, but I think this will result in a lot of ugly arrows that although can roughly point towards the correct directions, but really don't look nice when view as a whole.

Is there an algorithm, or even better, an existing code implementation ( in the form of well-documented APIs with Windows DLLs, or open source soure code), that allows me to generate the water "flow map" in an elegant, intuitive, look-nice way?

This is a well-studied problem in geomorphological and hydrological modeling, as well as the fields of geomorphometry and terrain analysis.

For visualization, flow accumulation is usually calculated. If every element of your terrain is taken as contributing a value of 1 to the flow, then flow accumulation is the sum of all the flow that passes through a given element. A depiction of this for a landscape might look like this:

There are several different flow routing methods. The one you describe is known as D8. A few additional ones are depicted below.

Algorithms for performing the above, along with citations to appropriate references, are available in my library, RichDEM.

• Looks impressive, but what does the legend actually stand for? May 18 '18 at 3:39
• In this case, number of upstream cells or portion thereof. May 18 '18 at 3:42
• So it would appear that the RED area is the lower ground and BLUE is the upper ground, based on the legend ( shouldn't it be the other way round to conform to the normal topography legend usage)? May 18 '18 at 4:36
• That's correct. Red has a higher flow accumulation (more upstream cells) so its value is higher. May 18 '18 at 4:50
• Interesting! Gotta check it out! May 18 '18 at 6:46

There is one program, called IBER, that fits into your request: http://www.iberaula.es/space/53/modules