Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Sunday, 16 January 2011

Colorful Menger Sponge in processing.py

Get processing.py here


Here is processing.py example of a Menger Sponge, where size is used to limit recursion.

"""
menger.py is a python script for use in processing.py
A version of my Menger Disco Version on openprocessing
Written by monkstone January 2010
"""

import math
import processing.opengl

XPOS = 0
YPOS = 1
ZPOS = 2
SZ = 3
angle = 0
ANGLE_STEP = math.pi/180
DATA = [-1, 0, 1]
MIN_SIZE = 30       
cubeData = [] # expose data at module level

COL_1 = color(170, 255, 126)
COL_2 = color(126, 255, 126)
COL_3 = color(255, 212, 126)
COL_4 = color(255, 42, 126)


def setup():
    """
    Is the processing setup
    """
    size(600, 600, OPENGL)
    __setupOpengl()
    __cube(0, 0, 0, height * 0.4)   # fill data array 
    
def draw():
    """
    Is the processing draw loop
    """
    background(200)
    lights()
    ambientLight(0.8, 0.8, 0.8)
    directionalLight(1.0, 1.0, 1.0, 1, 1, -1)
    global angle
    angle = ANGLE_STEP + angle % (math.pi * 2)
    translate(width/2, height/2, 0)
    rotateX(angle)
    rotateY(angle)
    rotateZ(angle)
    drawCubes()    
    
def __setupOpengl():
    """
    opengl options
    """
    hint(DISABLE_OPENGL_ERROR_REPORT)
    hint(ENABLE_OPENGL_4X_SMOOTH)


def __cube(x, y, z, sz):
    """
    Fill data array with cube data (position & size) recursively, missing
    menger holes, recursion limited by cube size
    """
    global cubeData 
    if (sz >= MIN_SIZE):
        u = sz/3.0
        for i in DATA:
            for j in DATA:
                for k in DATA:
                    if ((abs(i) + abs(j) + abs(k)) > 1):
                        __cube(x+i*u, y+j*u, z+k*u, u)

    else:       
         cubeData.append([x, y, z, sz])

def drawCubes():
    """
    render the cube data, calls __myBox()
    """
    for point in cubeData:
        pushMatrix()
        translate(point[XPOS], point[YPOS], point[ZPOS])
        __myBox(point[SZ])
        popMatrix()


def __myBox(sz):
    """
    __myBox can have individually colored faces/or blend
    """
    noStroke()
    beginShape(QUADS)
    # +Z "front" face
    fill(COL_1)
    vertex(-sz/2, -sz/2,  sz/2)
    fill(COL_2)
    vertex( sz/2, -sz/2,  sz/2)
    fill(COL_3)
    vertex( sz/2,  sz/2,  sz/2)
    fill(COL_4)
    vertex(-sz/2,  sz/2,  sz/2)
  
    # -Z "back" face
    fill(COL_1)
    vertex( sz/2, -sz/2, -sz/2)
    fill(COL_2)
    vertex(-sz/2, -sz/2, -sz/2)
    fill(COL_3)
    vertex(-sz/2,  sz/2, -sz/2)
    fill(COL_4)
    vertex( sz/2,  sz/2, -sz/2)
  
    # +Y "bottom" face
    fill(COL_1)
    vertex(-sz/2,  sz/2,  sz/2)
    fill(COL_2)
    vertex( sz/2,  sz/2,  sz/2)
    fill(COL_3)
    vertex( sz/2,  sz/2, -sz/2)
    fill(COL_4)
    vertex(-sz/2,  sz/2, -sz/2)
  
    # -Y "top" face
    fill(COL_1)
    vertex(-sz/2, -sz/2, -sz/2)
    fill(COL_2)
    vertex( sz/2, -sz/2, -sz/2)
    fill(COL_3)
    vertex( sz/2, -sz/2,  sz/2)
    fill(COL_4)
    vertex(-sz/2, -sz/2,  sz/2)
  
    # +X "right" face
    fill(COL_1)
    vertex( sz/2, -sz/2,  sz/2)
    fill(COL_2)
    vertex( sz/2, -sz/2, -sz/2)
    fill(COL_3)
    vertex( sz/2,  sz/2, -sz/2)
    fill(COL_4)
    vertex( sz/2,  sz/2,  sz/2)
  
    # -X "left" face
    fill(COL_1) 
    vertex(-sz/2, -sz/2, -sz/2)
    fill(COL_2)
    vertex(-sz/2, -sz/2,  sz/2)
    fill(COL_3)
    vertex(-sz/2,  sz/2,  sz/2)
    fill(COL_4)
    vertex(-sz/2,  sz/2, -sz/2)
  
    endShape()
    
def mouseReleased():
    noLoop()
    saveFrame("menger.png")
    loop()


No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2