Fractral Kubus

Fractal Kubus Tipe 2

Kode Program Processing

Box b;
ArrayList sponge;

void setup() {
size(800, 600, P3D);
sponge = new ArrayList();
Box b = new Box(0, 0, 0, 150);
sponge.add(b);
}

void mousePressed() {
ArrayList next = new ArrayList();
for (Box b : sponge) {
ArrayList newBoxes = b.generate();
next.addAll(newBoxes);
}
sponge = next;
>
}

void draw() {
background(0);
noFill();
stroke(0);
lights();
camera(mouseX, mouseY, (height/2) / tan(PI/4), width/2, height/2, 0, 0, 1, 0);
translate(width/2, height/2);
rotateX(0);
rotateY(-30);
rotateZ(0);
for (Box b : sponge) {
b.show();
}
}

class Box {
PVector pos;
float r;
Box(float x, float y, float z, float r_) {
pos = new PVector(x, y, z);
r = r_;
}

void show() {
pushMatrix();
translate(pos.x, pos.y, pos.z);
fill(0,255,255);
box(r);
popMatrix();
}

ArrayList generate() {
ArrayList boxes = new ArrayList();
for (int i=-1; i<2; i++) {
for (int j=-1; j<2; j++) {
for (int k=-1; k<2; k++) {
int sum = abs(i) + abs(j) + abs(k);
float newR = r/3;
if (sum <= 1) {
Box b = new Box(pos.x+i*newR, pos.y+j*newR, pos.z+k*newR, newR);
boxes.add(b);
}
}
}
}
return boxes;
}