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

Saturday, 15 January 2011

My Dome Fractal in processing.py

Get processing.py here.

"""
dome.py is a python script for use in processing.py
As far as I'm I aware this fractal is one of my own invention
Written by monkstone January 2010, features use of PeasyCam and
using the processing Matrix to store state cf a turtle stack
"""

import math
import processing.opengl
import peasy.PeasyCam as PeasyCam

# some globals
XPOS = 0
YPOS = 1
ANGLE = 2
DELTA = math.pi/4
distance = 40

RULES = {
'A' : 'F[&F+F+F+F+F+F+F+F+][A+]'
}

AXIOM = 'A+A+A+A+A+A+A+A'

production = ""   # need exposure at module level?

def __configureOpengl():
    """
    private setup opengl
    """
    hint(ENABLE_OPENGL_4X_SMOOTH)     
    hint(DISABLE_OPENGL_ERROR_REPORT) 
 
def __produce(axiom, rules):
    """
    private single pass rule substitution
    """
    output = ""
    for i in axiom:
        output += rules.get(i, i)    # second i is returned when no dict key found
    return output
    
 
def repeat(rpx, axiom, rules):
    """
    Repeat rule substitution in a recursive fashion rpx times
    """ 
    production = axiom
    for i in range(0, rpx):
        production = __produce(production, rules)
    return production
    
def render(production):        # avoiding switch and globals
    """
    Render evaluates the production string and calls box primitive
    uses affine transforms
    """
    lightSpecular(204, 204, 204) 
    specular(255, 255, 255) 
    shininess(1.0) 
    
    for val in production:
        if val == "F": 
            translate(0, distance/-2, 0)
            box(distance/9, distance, distance/9)
            translate(0, distance/-2, 0)
        elif val == "+": 
            rotateX(DELTA)
        elif val == "-": 
            rotateX(-DELTA)
        elif val == ">": 
            rotateY(-DELTA)
            repeat = 1
        elif val == "<": 
            rotateY(DELTA)
        elif val == "&": 
            rotateZ(-DELTA) 
        elif val == "^": 
            rotateZ(DELTA) 
        elif val == '[':
            pushMatrix()      
        elif val == ']':
            popMatrix()    
        else: 
            pass    


def setup():
    """
    Processing setup method
    """
    size(600, 600, OPENGL)
    __configureOpengl()
    cam = PeasyCam(this, 100) 
    cam.setMinimumDistance(200)
    cam.setMaximumDistance(500)
    global production
    production = repeat(3, AXIOM, RULES)
 
def draw():
    """
    Processing draw method
    """  
    background(0)
    lights()
    fill(255, 0, 0)
    render(production)
    
def mouseReleased():
    saveFrame("dome.png")

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