Skip to content

Repository files navigation

DiamondSquare.py

This is a python implementation of the classic diamond-square algorithm for 2-D height maps, which are often used for terrain generation and random map creation.

Feel free to post questions or requests, and I'll see what I can do.

If you use it for something interesting, I'd love to hear about it.

Best, -HKB-

Installation

  • Install using pip: pip install hkb_diamondsquare
  • Install from source git clone git@github.com:buckinha/DiamondSquare.git

Using it in a python project is easy:


fromhkb_diamondsquareimportDiamondSquareasDS#make a height map of size 16x20, with values ranging from 1 to 100, with moderate roughnessmap1=DS.diamond_square(shape=(16,20), min_height=1, max_height=100,
roughness=0.75)
#make a square height map with sides of length 120, that are VERY roughmap2=DS.diamond_square(shape=(120,120),
min_height=1,
max_height=100,
roughness=0.99)
#make a VERY smooth map with values from -10 to 10map3=DS.diamond_square(shape=(10,10), min_height=-10, max_height=10, roughness=0.05)
#use a specific random seed to ensure replicabilitysame_map_1=DS.diamond_square((15,15), 1, 100, 0.8, random_seed=1.234)
same_map_2=DS.diamond_square((15,15), 1, 100, 0.8, random_seed=1.234)
importnumpyasnpnp.allclose(same_map_1, same_map_2)
# prints Truedifferent_map=DS.diamond_square((15,15), 1, 100, 0.8, random_seed=1.2341, as_ndarray=True)
np.allclose(same_map_1, different_map)
# prints False

You can also vizualize your landscape using Seaborn (not included by default) as shown:

fromhkb_diamondsquareimportDiamondSquareimportseaborndefshow_terrain_2D(terrain_array):
seaborn.set()
ax=seaborn.heatmap(terrain_array)
map=DiamondSquare.diamond_square(shape=(50,50), min_height=0, max_height=10, roughness=0.3)
show_terrain_2D(map)

About

A python implementation of a diamond-square algorithm

Resources

Stars

30 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages