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:
Repeatedly letting the beetle place a cube of unit length and move the same length forward makes a square 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
).
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.
Skipping a cube randomly still yields a cube but sponge look and feel emerge.
Before attempting to turn it into a Menger sponge,
we wrap the conditional in a predicate named menger
as a preparation.
Another preparation, the floor
reporter using round
.
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.
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
.
If the level is greater than zero, we recalculate the range of a sub-sponge and recurse down one level (iterations - 1
).
Voila!
2015 My gh-pages