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

Tuesday, 1 May 2012

Non Recursive Menger Sponge Pyprocessing

from pyprocessing import *

FOV = PI/3.0
angle = 0.0
ANGLE_STEP = PI / 180.0
DATA = [-1,  0,  1]
       
def setup():
    size(800,600)
    cameraZ = (height/2.0) / tan(FOV/ 2.0)
    perspective(FOV, float(width)/float(height), cameraZ/10.0, cameraZ*10.0)
    
def draw():
    background(0,  0,  200)
    noStroke()
    stroke(0)
    lights()
    defineLights()
    translate(width/2.0, height/2.0, 0)
    global angle
    angle = (angle + ANGLE_STEP) % TWO_PI
    rotateZ(angle)
    rotateY(angle)
    create_menger_three(0, 0, 0, height/2.0)

def my_cube(xx,  yy,  zz,  sz):
    """
    Draw a cube with centre xx, yy, zz and size sz
    """
    sz *= 0.5
    beginShape(QUADS)
    normal(0, 0, 1)
    vertex(-sz + xx, -sz + yy, -sz + zz)
    vertex(+sz + xx, -sz + yy, -sz + zz)
    vertex(+sz + xx, +sz + yy, -sz + zz)
    vertex(-sz + xx, +sz + yy, -sz + zz)
    
    #Back face    
    normal(0, 0, -1)
    vertex(-sz + xx, -sz + yy, +sz + zz)
    vertex(+sz + xx, -sz + yy, +sz + zz)
    vertex(+sz + xx, +sz + yy, +sz + zz)
    vertex(-sz + xx, +sz + yy, +sz + zz)
    
    #Left face    
    normal(1, 0, 0)
    vertex(-sz + xx, -sz + yy, -sz + zz)
    vertex(-sz + xx, -sz + yy, +sz + zz)
    vertex(-sz + xx, +sz + yy, +sz + zz)
    vertex(-sz + xx, +sz + yy, -sz + zz)
    
    #Right face    
    normal(-1, 0, 0)
    vertex(+sz + xx, -sz + yy, -sz + zz)
    vertex(+sz + xx, -sz + yy, +sz + zz)
    vertex(+sz + xx, +sz + yy, +sz + zz)
    vertex(+sz + xx, +sz + yy, -sz + zz)
    
    #Top face    
    normal(0, 1, 0)
    vertex(-sz + xx, -sz + yy, -sz + zz)
    vertex(+sz + xx, -sz + yy, -sz + zz)
    vertex(+sz + xx, -sz + yy, +sz + zz)
    vertex(-sz + xx, -sz + yy, +sz + zz)
    
    #Bottom face    
    normal(0, -1, 0)
    vertex(-sz + xx, +sz + yy, -sz + zz)
    vertex(+sz + xx, +sz + yy, -sz + zz)
    vertex(+sz + xx, +sz + yy, +sz + zz)
    vertex(-sz + xx, +sz + yy, +sz + zz)
    endShape()


def create_menger_three(xx, yy, zz, sz):
    """
    Create a non recursive menger sponge using create_menger
    """ 
    u = sz / 3.0
    for i in DATA:
        for j in DATA:
            for k in DATA:
                if ((abs(i) + abs(j) + abs(k)) > 1):
                    create_menger(xx + (i * u), yy + (j * u), zz + (k * u), u)

def create_menger(xx, yy, zz, sz):
    """
    Create a non recursive menger sponge using unit_menger
    """ 
    u = sz / 3.0
    for i in DATA:
        for j in DATA:
            for k in DATA:
                if ((abs(i) + abs(j) + abs(k)) > 1):
                    unit_menger(xx + (i * u), yy + (j * u), zz + (k * u), u)

def unit_menger(xx, yy, zz, sz):
    """
    Create a unit menger sponge using my_cube
    """ 
    u = sz / 3.0
    for i in DATA:
        for j in DATA:
            for k in DATA:
                if ((abs(i) + abs(j) + abs(k)) > 1):
                    my_cube(xx + (i * u), yy + (j * u), zz + (k * u), u)
                        
def defineLights():
    """
    Without lights you wouldn't see the menger
    """
    ambientLight(50, 50, 50)
    pointLight(150, 100, 0, 200, -150, 0)
    directionalLight(0, 102, 255, 1, 0, 0)
    spotLight(255, 255, 109, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)     
 run()

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