Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Diamond Square — Terrain Generator in C

A minimal C implementation of the Diamond-Square algorithm for procedural terrain generation. Produces heightmaps rendered as 1024x1024 images using SDL2.


Examples

Generated terrain 1 Generated terrain 2 Generated terrain 3

Requirements

  • GCC or any C compiler
  • SDL2

On Debian/Ubuntu:

sudo apt install libsdl2-dev

On macOS with Homebrew:

brew install sdl2

Build

gcc main.c -o out -lSDL2

Run

./out

Algorithm

The Diamond-Square algorithm is a method for generating realistic fractal terrain. Starting from a 2D grid with randomized corner values, it proceeds in two alternating steps:

  • Diamond step — for each square in the grid, compute the midpoint and assign it the average of the four corners plus a random offset.
  • Square step — for each diamond, compute the midpoint of each edge and assign it the average of the surrounding points plus a random offset.

The random offset is scaled down at each iteration, producing a natural-looking roughness that decreases at finer scales. The result is a heightmap with fractal properties similar to real terrain.


References

About

2D Procedural Map Generator based on Diamond-Square Algorihtm and SDL2

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages