# Generate planet textures using the Fault Formation algorithm

Perhaps someone remembers the wonderful old-school space game Star Control 2. At one time, I was struck by a huge star map with uncharted planets that were to be explored against the backdrop of an unfolding global catastrophe. Since the authors published the source codes, the game has been ported under the new name The Ur-Quan Masters to most modern platforms.

Rummaging around in the source, I discovered a simple algorithm that generates the textures of the planets, and wrote a program in Python that allows you to generate similar textures.

# Algorithm

1. We form a height map using the Fault Formation algorithm ( “fault formation” )
2. Color the height map using reference RGB colors and the gradient between them

### Formation of heights map

1. Create a matrix of heights of the same dimension as the generated texture of the planet. North is above, south is from below, and the horizontal of the matrix is ​​a circular scan of the planet along the parallel. The value of the heights can only vary from 0 to 255, so it’s convenient to display them in shades of gray
2. Fill the matrix with a base elevation , for example, 128:

3. We generate two random disjoint lines from north to south. These lines “form a fault” - they divide the planet's surface into two parts, one of which we raise to a fixed constant ( elevation delta ), and the other we lower to the same constant. For clarity, let the constant be 10:

4. We repeat the previous paragraph a specified number of times ( iterations num ).
In the figures below: 10 iterations, 100 iterations, 1000 iterations:

### Coloring a height map with RGB colors

1. Divide the entire range of heights (from 0 to 255) into N more or less equal parts with N + 1 reference points at the boundaries of these parts
2. Set the RGB color for each reference point in the range.
3. We calculate the RGB color for all the intermediate points of the range, linearly interpolating the color components between the anchor points. In other words, fill the heights with a gradient between the reference colors
4. We generate the texture of the planet, replacing each cell of the DEM with the RGB color calculated in the previous paragraph

Here are some possible options for coloring the same height map:

# Program

To run the program, you need to install Python 2.7 and several libraries for it: NumPy , PIL and PyOpenGL.