#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Fractals

edited March 2016

Hi guys,

does anyone have any tips on how to incorporate 3-D fractals? I've already managed to make a few 2-D ones show but I eventually want to get to a full 3D one whose variables I have control over because I want to synchronize it wit some audio. I'd love any recommendations on how to get myself started.

Many thanks

Tagged:

• What kind of 3D fractal? There are many, many kinds and the procedures to represent them often have nothing to do with one another

• Wheeee! 3D fractals are pretty!

``````void setup() {
size(600, 600, P3D);
noStroke();
fill(64,64,196);
}

void draw() {
background(0);
translate(width/2, height/2, 0);
scale(100);
lights();
rotateY(map(mouseX, 0, width, -PI, PI));
rotateX(map(mouseY, 0, height, -PI, PI));
fractalBox(2);
}

void fractalBox(int level) {
box(1);
if (level<=0) return;
for (int x=-1;x<2;x+=2) {
for (int y=-1;y<2;y+=2) {
for (int z=-1;z<2;z+=2) {
pushMatrix();
translate(x, y, z);
scale(1/3f);
fractalBox(level-1);
popMatrix();
}
}
}
}
``````
• I was asked in a private message to convert this to use an ArrayList. Here is the result:

``````class Spacube {
PVector p;
float s;
color c;
Spacube(float px, float py, float pz, float is) {
p = new PVector(px, py, pz);
s = is;
c = color(random(255), random(255), random(255));
}
void draw() {
pushMatrix();
translate(p.x, p.y, p.z);
fill(c);
box(s);
popMatrix();
}
}

ArrayList<Spacube> spacubes = new ArrayList();

void setup() {
size(600, 600, P3D);
noStroke();
fractalBox(3, 0, 0, 0, 300);
}

void draw() {
background(0);
translate(width/2, height/2, 0);
lights();
rotateY(map(mouseX, 0, width, -PI, PI));
rotateX(map(mouseY, 0, height, -PI, PI));
for (int i=0; i<spacubes.size (); spacubes.get(i++).draw());
}

void fractalBox(int level, float rx, float ry, float rz, float rs) {
float s = rs/3f;
spacubes.add( new Spacube(rx, ry, rz, s) );
if (level<=0) return;

for (int x=-1; x<2; x+=2) {
for (int y=-1; y<2; y+=2) {
for (int z=-1; z<2; z+=2) {
fractalBox(level-1, rx+(x*s), ry+(y*s), rz+(z*s), s);
}
}
}
}
``````