# Python Code (Using Processing) # Jonathan Frech 31st of August, 2016 # import import random # box class class Box(): # init def __init__(self, x, y, z, siz, r, g, b): # pos self.x = x self.y = y self.z = z # size self.siz = siz # color self.r, self.g, self.b = r+(random.randint(0, 1)*2-1)*10, g+(random.randint(0, 1)*2-1)*10, b+(random.randint(0, 1)*2-1)*10 self.r = max(0, min(255, self.r)) self.g = max(0, min(255, self.g)) self.b = max(0, min(255, self.b)) # render box def render(self): # save pushMatrix() # move, paint and set stroke translate(self.x, self.y, self.z) fill(self.r, self.g, self.b) noStroke() # draw actual box box(self.siz) # reset popMatrix() # generate new, smaller boxes def gen(self): n = self.siz/3. b = [] for x in range(-1, 2): for y in range(-1, 2): for z in range(-1, 2): if abs(x)+abs(y)+abs(z) > 1: b.append(Box(self.x+x*n, self.y+y*n, self.z+z*n, n, self.r, self.g, self.b)) return b # generate new cube tier on space bar def keyTyped(): # global variables global boxes, tier if key == " ": tier += 1 nboxes = [] for box in boxes: if True or len(nboxes) < 10000: for b in box.gen(): nboxes.append(b) boxes = nboxes # reset rotation """elif key == "p": global anglex, angley, anglez anglex, angley, anglez = 0, 0, 0""" # setup def setup(): # global variables global boxes, anglex, angley, anglez, tier # screen size size(1080, 720, P3D) # first box boxes = [Box(0, 0, 0, 300, random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))] # rotation and current tier anglex, angley, anglez = 0, 0, 0 tier = 0 # caption frame.setTitle("Menger Spong -- J") # draw def draw(): # global variables global boxes, anglex, angley, anglez # background color background(60, 60, 130) # save pushMatrix() # graphics setup lights() translate(width/2, height/2) rotateX(anglex) rotateY(angley) rotateZ(anglez) # draw boxes for b in boxes: b.render() # reset popMatrix() # text textSize(32) translate(0, 0, 0) fill(255, 255, 255) text("Tier " + str(tier), 10, 30, 0) # rotate further anglex += .001 angley += .002 anglez += .003