ArticleslgStudy

computer science

Marching squares

Marching squares is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Marching squares rather than just read about it. In short: In computer graphics, marching squares is an algorithm that generates contours for a two-dimensional scalar field (rectangular array of individual numerical values). A similar method can be used to contour 2D triangle meshes.

Marching squares — main illustration
Marching squares — illustration

Key takeaways

  • Marching squares belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Marching squares to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Marching squares from memory before moving on to harder problems.

Reference excerpt

In computer graphics, marching squares is an algorithm that generates contours for a two-dimensional scalar field (rectangular array of individual numerical values). A similar method can be used to contour 2D triangle meshes. The contours can be of two kinds:

Isolines – lines following a single data level, or isovalue. Isobands – filled areas between isolines. Typical applications include the contour lines on topographic maps or the generation of isobars for weather maps. Marching squares takes a similar approach to the 3D marching cubes algorithm:

Process each cell in the grid independently. Calculate a cell index using comparisons of the contour level(s) with the data values at the cell corners. Use a pre-built lookup table, keyed on the cell index, to describe the output geometry for the cell. Apply linear interpolation along the boundaries of the cell to calculate the exact contour position.

Basic algorithm Here are the steps of the algorithm: Apply a threshold to the 2D field to make a binary image containing:

1 where the data value is above the isovalue 0 where the data value is below the isovalue Note: Data equal to the isovalue has to be treated as above or below in a consistent way. Every 2x2 block of pixels in the binary image forms a contouring cell, so the whole image is represented by a grid of such cells (shown in green in the picture below). Note that this contouring grid is one cell smaller in each direction than the original 2D field. For each cell in the contouring grid:

Compose the 4 bits at the corners of the cell to build a binary index: walk around the cell in a clockwise direction appending the bit to the index, using bitwise OR and left-shift, from most significant bit at the top left, to least significant bit at the bottom left. The resulting 4-bit index can have 16 possible values in the range 0–15. Use the cell index to access a pre-built lookup table with 16 entries listing the edges needed to represent the cell (shown in the lower right part of the picture below). Apply linear interpolation between the original field data values to find the exact position of the contour line along the edges of the cell.

Disambiguation of saddle points The contour is ambiguous at saddle points. It is possible to resolve the ambiguity by using the average data value for the center of the cell to choose between different connections of the interpolated points (four images in bottom-right corner):

Isobands A similar algorithm can be created for filled contour bands within upper and lower threshold values:

Contouring triangle meshes The same basic algorithm can be applied to triangular meshes, which consist of connected triangles with data assigned to the vertices. For example, a scattered set of data points could be connected with a Delaunay triangulation to allow the data field to be contoured. A triangular cell is always planar, because it is a 2-simplex (i.e. specified by n+1 vertices in an n-dimensional space). There is always a unique linear interpolant across a triangle, and no possibility of an ambiguous saddle.

Isolines The analysis for isolines over triangles is especially simple: there are 3 binary digits, so there are 8 possibilities:

Isobands The analysis for isobands over triangles requires 3 ternary trits, so there are 27 possibilities:

Dimensions and spaces The data space for the Marching Squares algorithm is 2D, because the vertices assigned a data value are connected to their neighbors in a 2D topological grid, but the spatial coordinates assigned to the vertices can be in 2D, 3D or higher dimensions. For example, a triangular mesh may represent a 2D data surface embedded in 3D space, where spatial positions of the vertices and interpolated points along a contour will all have 3 coordinates. Note that the case of squares is ambiguous again, because a quadrilateral embedded in 3-dimensional space is not necessarily planar, so there is a choice of geometrical interpolation scheme to draw the banded surfaces in 3D.

Performance considerations The algorithm is embarrassingly parallel, because all cells are processed independently. It is easy to write a parallel algorithm assuming:

Shared read-only input scalar field. Shared append-only geometry output stream. A naive implementation of Marching Squares that processes every cell independently will perform every linear interpolation twice (isoline) or four times (isoband). Similarly, the output will contain 2 copies of the 2D vertices for disjoint lines (isoline) or 4 copies for polygons (isobands). [Under the assumptions that: the grid is large, so that most cells are internal; and a full contiguous set of isobands is being created.] It is possible to reduce the computational overhead by caching the results of interpolation. For example, a single-threaded serial version would only need to cache interpolated results for one row of the input grid. It is also possible to reduce the size of the output by using indexed geometric primitives, i.e. create an array of 2D vertices and specify lines or polygons with short integer offsets into the array.

… excerpt ends here. Continue reading the full article.

Illustrations

Marching squares illustration
Marching squares illustration
Marching squares illustration
Marching squares illustration

Worked examples

Example 1 — a first encounter with Marching squares

Start with the simplest possible case. Write down what Marching squares claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Marching squares before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Marching squares ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Marching squares

In research
Marching squares appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Marching squares in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Marching squares is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer graphics algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Marching squares outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Marching squares” →

How to study Marching squares in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Marching squares means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Marching squares out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Marching squares in simple terms?

In computer graphics, marching squares is an algorithm that generates contours for a two-dimensional scalar field (rectangular array of individual numerical values). A similar method can be used to contour 2D triangle meshes.

Why does Marching squares matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Marching squares?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Marching squares.

Tags

  • Computer graphics algorithms

Keep exploring