The Menger Sponge, Complement and Associate

A few months ago, I had never heard of Karl Menger or the cube-like fractal that he invented. Now I have this long blog post about the sponge, and an extension that I am calling its "associate".

Contents

Complements and associates

The complement of the Menger sponge fractal at a given level is all the material that is cut away to reach that level. The sponge associate is something I encountered while trying to compute the sponge complement.

Code comes first

我坚信认真写计算机code is a means for people to communicate with each other, as well as control machines. Some of the images that I am about to present can be seen elsewhere on the Web, but I am most pleased with the code that produces them, so I will describe it first.

Here is the main routine,sponge. If you have been following this blog, you will have seensome of it before. At the command line,spongeexpects only two input arguments, a nonnegative integer specifying the level and a boolean variable specifying the Menger sponge or its complement. The sponge is displayed with a dark shade of gold and the complement with a light shade. The functionspongeis recursive and the recursive calls have more than two arguments.

Level zero terminates the recursion and is dealt with first. The level zero sponge isa solid cube. The level zero complement is empty and we just display the outline of a cube. Ultimately, all the output is produced by function callscube(d,w,c), which displays a dark or light cube with half-widthwcentered atc = [x y z], so nothing but cubes of various sizes in various locations are displayed. Ordinarily the cube is merged with whatever is in the current figure, thus displaying the union of the two sets. Aclacommand clears the current display axis and thereby initiates a new set.

If the level is not zero, we reach the nested loops and the recursive calls. The variablesx,yandztake on all possible combinations of-2,0and+2, so the innermost statements are executed 27 times. Now the key variablenzcomes into play. It is the number of nonzeros in[x y z]. The vector[0 0 0]has zero nonzeros and is in the center of some small cube. Vectors like[2 0 0]and[0 -2 0]have one nonzero and are in the center of some face of that small cube. Vectors like[2 0 -2]有两个非零是一些边缘的中间nd vectors like[-2 2 -2]with three nonzeros are at the corners. There are seven[x y z]'s withnz < 2leaving 20 withnz >= 2. This provides the trisecting rule for generating the next level Menger sponge.

typesponge
function sponge(m,s,d,w,c) % sponge(m,s) % m: level, 20^m cubes. % s: sponge, dark gold or complement, light gold. % % Resursive usage: % sponge(m,s,d,w,c) % d: dark or light. % w: half-width. % c: center. if nargin == 2 init_fig(5) sponge(m,s,s,3,[0 0 0]) elseif m == 0 && s cube(d,w,c) elseif m == 0 cube([],w,c) else w = w/3; c = c/3; for x = [-2 0 2] for y = [-2 0 2] for z = [-2 0 2] nz = nnz([x y z]); if s && nz >= 2 sponge(m-1,s,d,w,c+[x y z]) elseif ~s && nz < 2 sponge(m-1,~s,d,w,c+[x y z]) end end end end end end

Level 0

This is the level 0 sponge, a solid cube.

cla sponge(0,true)

This is the level 0 complement. It is empty.

cla sponge(0,false)

Level 1

三等分0级分成子集要= 27个小立方体。▽ete the center cube and the center of each face. This is the level 1 sponge with 20 smaller cubes and seven holes.

cla sponge(1,true)

This is the level 1 complement, the seven missing cubes.

cla sponge(1,false)

If you could somehow put the level 1 sponge and the level 1 complement together again, you would recreate a slightly used, but full, cube.

cla sponge(1,true) sponge(1,false)

Level 1 is as far as I need to golate last yearin order to decorate mySeason's Greetings.

Level 2

The level 2 sponge has20^2 = 400cubes. I got this far in level 2 formy first postabout the Menger sponge.

cla sponge(2,true)

The next figure has never appeared before inCleve's Corner. I call it the level 2partial complement.

cla sponge(2,false)

Form the union of the level 2 partial complement and the level 1 complement to give the entire level 2 complement.

sponge(1,false)

The union of the level 2 sponge and its complement is a full cube.

sponge(2,true)

Level 3

The is the level 3 Menger sponge. It is made from20^3 = 8000small cubes. Its volume is less than half that of the original cube.

cla sponge(3,true)

The is the level 3 partial complement.

cla sponge(3,false)

Combine levels 1, 2 and 3 partial complements to produce the entire level 3 complement.

sponge(2,false) sponge(1,false)

The union of the level 3 sponge and its complement is, again, full.

sponge(3,true)
formatcompactformatshort

Ten levels

I don't have enough time and my laptop doesn't have enough memory to go much beyond level 3, but if we could continue trisecting, the level 10 sponge would be made out of20^10 = 10,240,000,000,000very tiny cubes.

Levelmwould reduce the volume of the level0cube by a factorr.^mwhere

r = (20/27)
r = 0.7407

By level 10 the volume of the sponge would be only 5 percent of the original volume, and the remaining 95 percent would be in the complement.

m = (0:10)'; volumes = [r.^m 1-r.^m];
fprintf('\n\n volume\n') fprintf(' level sponge complement\n') fprintf('%10d %8.3f %8.3f\n',[m volumes]')
volume level sponge complement 0 1.000 0.000 1 0.741 0.259 2 0.549 0.451 3 0.406 0.594 4 0.301 0.699 5 0.223 0.777 6 0.165 0.835 7 0.122 0.878 8 0.091 0.909 9 0.067 0.933 10 0.050 0.950
plot(m,volumes,'o-','linewidth',1.0) xlabel('level') ylabel('volume') legend({'sponge','complement'},'position',[.67 .47 .21 .08])
close

Associate

A Web dictionary says that anassociateis someone with limited or subordinate membership in an organization. We are all familiar with associate professors in academic organizations. I accidentally created these sponge associates when I was learning how to compute sponge complements. I liked the images, so I have kept them around.

The code that I now have for generating associates is almost the same as the code for generating sponges. Just count the number of zeros in a coordinate vector instead of the number of nonzeros.

dbtype28spongedbtype28associate
28 nz = nnz([x y z]); 28 nz = 3 - nnz([x y z]);

Level 2

At levels 0 and 1, sponges and associates construct the same objects. But at level 2 we have this fellow with 49 cubes.

cla associate(2,true)

The associates have their own complements. The cube count at levelmis7^mfor an associate and20*7^(m-1)for its complement. This one at level 2 has 149 cubes.

cla associate(2,false)

Level 3

Here is the level 3 associate.

cla associate(3,true)

And the level 3 associate complement.

cla associate(3,false)

Level 4

The level 4 images are best viewed with a larger format. If you want to see them, click on these links.

https://blogs.mathworks.com/cleve/files/level_4_true.png

https://blogs.mathworks.com/cleve/files/level_4_false.png




Published with MATLAB® R2021a

|

コメント

コメントを残すには、ここをクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。