0
\$\begingroup\$

This question already has an answer here:

Challenge

My challenge, is for you to generate a Menger Sponge based on the level/iteration given. You need to draw it in 3d, anyway you can.

Examples

Inputs: 0, 1, 2, 3

Outputs:

Diagram


Background Information

What is a Menger Sponge

In mathematics, the Menger sponge (also known as the Menger universal curve) is a fractal curve. It is a three-dimensional generalization of the Cantor set and Sierpinski carpet

Properties

See https://en.wikipedia.org/wiki/Menger_sponge#Properties (too long to copy and paste)

How do I construct the sponge?

Diagram

  1. Begin with a cube (first image).

  2. Divide every face of the cube into 9 squares, like a Rubik's Cube. This will sub-divide the cube into 27 smaller cubes.

  3. Remove the smaller cube in the middle of each face, and remove the smaller cube in the very center of the larger cube, leaving 20 smaller cubes (second image). This is a level-1 Menger sponge (resembling a Void Cube).

  4. Repeat steps 2 and 3 for each of the remaining smaller cubes, and continue to iterate ad infinitum.

The second iteration gives a level-2 sponge (third image), the third iteration gives a level-3 sponge (fourth image), and so on. The Menger sponge itself is the limit of this process after an infinite number of iterations.

Credit

Background info taken from this wikipedia page on Menger Sponges.


Good Luck!

Remember this is the shortest program wins!

\$\endgroup\$

marked as duplicate by user42649, MD XF, Digital Trauma code-golf Jun 13 '17 at 21:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ @HyperNeutrino 3d is now required, so it's not a duplicate. \$\endgroup\$ – Noah Cristino Jun 13 '17 at 21:13
  • 1
    \$\begingroup\$ you shouldn't change the question when 2 of us answered \$\endgroup\$ – J42161217 Jun 13 '17 at 21:16
  • \$\begingroup\$ @Jenny_mathy I don't have a choice since doing it in 2d is already a question. \$\endgroup\$ – Noah Cristino Jun 13 '17 at 21:17
  • \$\begingroup\$ I'm voting to reopen simply because this is no longer a dupe. However, you will need to tighten up the spec to allow us to onow exactly what kind of 3d output you want \$\endgroup\$ – Beta Decay Jun 13 '17 at 21:17
  • 1
    \$\begingroup\$ Since 3D is now required (invalidating existing answers), I think the question is now unclear. There is a lot you need to specify for 3D including but not limited to viewing angle, projection, lighting, shading. \$\endgroup\$ – Digital Trauma Jun 13 '17 at 21:18
4
\$\begingroup\$

anyway.. here is the 3D answer

Mathematica, 15 bytes

#~MengerMesh~3&

new built-in in Mathematica 11.1

input

3

output

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Can you add a try it link? \$\endgroup\$ – Noah Cristino Jun 13 '17 at 21:16
  • \$\begingroup\$ Why don't you just post it on codegolf.stackexchange.com/questions/40104/sierpinski-carpets since your code generates Sierpinski Carpets (2d menger sponges), not Menger Sponges, which my challenge is about. \$\endgroup\$ – Noah Cristino Jun 13 '17 at 21:43
  • 2
    \$\begingroup\$ I answered the 3D question ,too.Next time just ask "one" question in order to receive one answer \$\endgroup\$ – J42161217 Jun 13 '17 at 21:47
  • \$\begingroup\$ Why would you ever need a built-in function to do that either then now? \$\endgroup\$ – Noah Cristino Jun 13 '17 at 22:06
  • 1
    \$\begingroup\$ @NoahCristino Ask that to all 5000 of Mathematica's builtins :P \$\endgroup\$ – HyperNeutrino Jun 18 '17 at 2:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.