By year

  1. 2018 (2)
  2. 2017 (4)
  3. 2015 (9)

Making a Menger Sponge with BeetleBlocks

3D Graphics BeetleBlocks Visual Programming Fractal

Posted on Sep 3

My step by step process of making a Menger Sponge of level 3 using BeetleBlocks, a visual programming environment for 3D design that everyone can enjoy via the Web browser:

Youtube video

Menger Sponge Level 3 with BeetleBlocks - YouTube


Repeatedly letting the beetle place a cube of unit length and move the same length forward makes a square bar.

Script for Bar Bar

It can trivially turn into a plate by nesting the bar making process within the outer repeat with the y-coordinate shift (change absolute y by 1).

Script for Plate Plate

Nesting once more in the same way produces a (bigger) cube. Moving around a block of code without breaking its syntactic structure is where a visual programming language really shines.

Script for Cube Cube

Skipping a cube randomly still yields a cube but sponge look and feel emerge.

Script for Sponge Cube

Before attempting to turn it into a Menger sponge, we wrap the conditional in a predicate named menger as a preparation.

Script for Predicate

Another preparation, the floor reporter using round.

Script for Floor

Now the groundwork is done, we try implementing the Menger sponge of level 1 (iteration = 1). The interface of the menger predicate is tabulated below.

Input Expected Actual Value
x, y, z The position of the beetle.
iterations The number of iterations (The level) of a Menger sponge.
x1 The starting x-coordinate (inclusive) of the sponge.
x2 The terminating x-coordinate (exclusive) of the sponge.
y1 The starting y-coordinate (inclusive) of the sponge.
y2 The terminating y-coordinate (exclusive) of the sponge.
z1 The starting z-coordinate (inclusive) of the sponge.
z2 The terminating z-coordinate (exclusive) of the sponge.
Output Condition
true The given point lies in a cell constituting the sponge (not in a hole).
false The given point is in a hole of the sponge.

A "hole" is made if two of the coordinates lie in middle thirds.

Script for Menger Sponge Level 1 Cube

The final step is making the menger predicate recursive. The recursion terminates if the iterations (level) equals to zero. A level-0 Menger sponge is just a cube without any hole in it. Hence, the predicate returns true.

Script for Recursion Termination

If the level is greater than zero, we recalculate the range of a sub-sponge and recurse down one level (iterations - 1).

Script for Recursion


Menger Sponge Level 3

2015 My gh-pages