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

Friday, 14 January 2011

MPeano fractal using lsystems in processing.py

Get processing.py here

"""
mpeano.py is a python script for use in processing.py. 
by monkstone features length adjustment and reversing
"""

import math

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

RULES = {
'F' : '',
'Y': 'FFY',
'X' : '+!X!FF-BQFI-!X!FF+',
'A' : 'BQFI',
'B' : 'AFF' 
}

AXIOM = 'XFF2-AFF2-XFF2-AFF'


def produce(axiom, rules):
    """
    single rule substitution utility
    """
    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 draw_line
    """
    global DELTA
    distance = 15
    turtle = [width/10, height/10, -DELTA]
    repeat = 1
    for val in production:
        if val == "F": 
            turtle = draw_line(turtle, distance)
        elif val == "+": 
            turtle[ANGLE] += DELTA * repeat
            repeat = 1
        elif val == "-": 
            turtle[ANGLE] -= DELTA * repeat
            repeat = 1
        elif val == "I": 
          distance *= 1/math.sqrt(2)
        elif val == "Q": 
            distance *= math.sqrt(2)
        elif val == "!":             
            DELTA = -DELTA        
        elif (val == '2'):
            repeat = 2   
        else: 
            pass


def draw_line(turtle, length):
    """
    Draw line utility uses processing 'line' function to draw lines
    """
    new_xpos = turtle[XPOS] + length * math.cos(turtle[ANGLE])
    new_ypos = turtle[YPOS] - length * math.sin(turtle[ANGLE])
    line(turtle[XPOS], turtle[YPOS], new_xpos, new_ypos)
    return [new_xpos, new_ypos, turtle[ANGLE]]     


def setup():
    """
    The processing setup statement
    """
    size(600, 600)
    production = repeat(6, AXIOM, RULES)
    background(0, 0, 255)
    smooth()
    stroke(255, 255, 0)
    strokeWeight(3)
    render(production)    


def draw():
    """
    An empty processing draw statement seems to be required to prevent premature exit()?
    """
    pass 



 

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