We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Menger Sponge
Page Index Toggle Pages: 1
Menger Sponge (Read 764 times)
Menger Sponge
Aug 10th, 2007, 8:07pm
 
Here's the source to a slightly complicated version of a Menger Sponge (see http://en.wikipedia.org/wiki/Menger_sponge ) renderer:

/***************
***BEGIN CODE***
***************/

boolean drawing;
float xr = 0;
float yr = random(0,TWO_PI);
color cubeColor;
int cubeCount;
int stages=2;
int pstages;

void setup()
{
 size(640, 480, P3D);
 noStroke();
 colorMode(HSB,1.0);
 cubeColor = color(random(0.0,1.0), 1.0, 1.0);
 drawing = true;
 pstages = stages;
 stages = 0;
}

void draw()
{
 if(drawing)
 {
   background(0);
   cubeMain();
   if(!mousePressed)
     if(stages<pstages)
       stages++;
     else
       drawing = false;
 }
 else if(stages != pstages)
 {
   drawing = true;
 }
}

void keyPressed()
{
 switch(keyCode)
 {
   case UP:
     pstages++;
     break;
   case DOWN:
     if(pstages>0)
       pstages--;
     break;
   case RIGHT:
     cubeColor = color(hue(cubeColor)+0.01, saturation(cubeColor), brightness(cubeColor));
     stages = 0;
     break;
   case LEFT:
     cubeColor = color(hue(cubeColor)-0.01, saturation(cubeColor), brightness(cubeColor));
     stages = 0;
     break;
 }
}

void mouseDragged()
{
 float rate = 0.01;
 yr += (pmouseX-mouseX) * rate;
 xr += (mouseY-pmouseY) * rate;
 stages = 0;
 drawing = true;
}

void cubeMain()
{
 cubeCount=0;
 lights();
 translate(width/2, height/2, 0);
 rotateX(xr);
 rotateY(yr);
 cube(0, 0, 0, 200, stages);
}

void cubes(float x, float y, float z, float s, int n)
{
 float u = s/3;
 for(float i=-u; i<=u; i+=u)
 {
   for(float j=-u; j<=u; j+=u)
   {
     for(float k=-u; k<=u; k+=u)
     {
       if(abs(i) + abs(j) + abs(k) >= 2*u)
       {
         cube(x+i, y+j, z+k, u, n);
       }
     }
   }
 }
}

void cube(float x, float y, float z, float s, int n)
{
 if(n>0)
 {
   n--;
   cubes(x, y, z, s, n);
 }
 else
 {
   pushMatrix();
   translate(x, y, z);
   cubeCount++;
   fill((hue(cubeColor) + cubeCount / (pow(20,stages) * 5)) % 1.0, saturation(cubeColor), brightness(cubeColor));
   box(s);
   popMatrix();
 }
}

/***************
****END CODE****
***************/

And here's the source for a simple enough example to be used in Learning, for example:

/***************
***BEGIN CODE***
***************/

boolean drawing;
float xr = 0;
float yr = random(0,TWO_PI);
int stages = 2;

void setup()
{
 size(200, 200, P3D);
 noStroke();
 fill(200);
 drawing = true;
}

void draw()
{
 if(drawing)
 {
   background(0);
   lights();
   translate(width/2, height/2, 0);
   rotateX(xr);
   rotateY(yr);
   cube(0, 0, 0, 100, stages);
   drawing = false;
 }
}

void mouseDragged()
{
 float rate = 0.01;
 yr += (pmouseX-mouseX) * rate;
 xr += (mouseY-pmouseY) * rate;
 drawing = true;
}

void cube(float x, float y, float z, float s, int n)
{
 if(n>0)
 {
   n--;
   float u = s/3;
   for(float i=-u; i<=u; i+=u)
     for(float j=-u; j<=u; j+=u)
       for(float k=-u; k<=u; k+=u)
         if(abs(i) + abs(j) + abs(k) >= 2*u)
           cube(x+i, y+j, z+k, u, n);
 }
 else
 {
   pushMatrix();
   translate(x, y, z);
   box(s);
   popMatrix();
 }
}

/***************
****END CODE****
***************/

I hope you love this fractal as much as I do.

-- Lukstr
Page Index Toggle Pages: 1