Marching Squares Simulation in C++ ImGui

Markus Buchholz
2 min readApr 17, 2023

--

Marching squares is an inspiring algorithm applicable in many domains (mainly in computer graphics) which is used for contours generation in a 2D scalar field.

This article explains how the contours are created and gives you the possibility to explore C++ simulation which I share on my GitHub.

The principle of creating matching squares is straightforward but delivers a marvelous experience for human perception. Having the 2D grid of points (vertexes), the random engine uses the values of 0 or 1 for each vertex. Here I would like to take note since the random assignment can be performed based on other random engines or even noise (Perlin noise which I discussed here).

Having the vertex assignment we have to perform the analysis of squares ( four vertexes) and classify the square, according to the values of each vertex (0 or 1) to one of the 16 classes.
Generally speaking, we separate square vertexes into two groups: Zeros and Ones.
From the algorithm perspective, we simply assign a certain pattern to the class and later draw lines according to algorithm implementation (specification which you can see on the Wikipedia page).

Marching squares creation. (by author)

If you applied all your grid descriptions about the method you can expect the following results

Static marching squares (by author)

Here is the simulation in ImGui C++,

Simulation (by author)

The implementation of the solution I deployed in C++ with the visualization in ImGui. The introduction to the C++ ImGui and information on how to compile and build programs you will find in one of my previous articles.

--

--