procgenlib.synthesis package

procgenlib.synthesis.diamond_square(rng, square_size, num_squares, primary_scale, roughness, base_level=0)

Generate a fractal terrain using the Diamond-square algorithm.

This function creates a heightmap of a fractal terrain using the Diamond-square algorithm. The generated terrain is represented as a NumPy array.

Note the algorithm is known to produce some unnatural patterns in the output; see the link above for more details.

Additional background on distribution of elevation in the real world can be found in the following article: Distribution of Elevations: New in Wolfram Language 12.

Parameters:
  • rng (Generator) – A NumPy random number generator for reproducible randomness.

  • square_size (int) – The edge length of the basic square.

  • num_squares (Tuple[int, int]) – The number of squares to generate along each axis.

  • primary_scale (float | ndarray) – The primary scaling factor(s) for height variation. This can be a single float or a NumPy array matching the terrain dimensions.

  • roughness (float | ndarray) – The roughness factor(s) for height variation. This can be a single float or a NumPy array matching the terrain dimensions.

  • base_level (float) – The base level height for the terrain. (default 0)

Returns:

A 2D NumPy array representing the generated terrain heightmap. Its dimensions are num_squares[0] * square_size + 1 by num_squares[1] * square_size + 1 samples.

Return type:

np.ndarray

Example:

Generate a heightmap with specified parameters:

import numpy as np
from numpy.random import Generator, PCG64
from procgenlib.synthesis import diamond_square

# Create a random number generator
rng = Generator(PCG64(12345))

# Generate the heightmap
heightmap = diamond_square(rng,
                           square_size=8,
                           num_squares=(1, 1),
                           primary_scale=1,
                           roughness=1)