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

Wednesday, 9 February 2011

Using a dictionary of functions in processing.py (in place of switch)

Here is my attempt at using a dictionary of functions in processing.py

   1 """
   2 sierpinski.py is a python script for use in processing.py.
   3 A very simple lsystem example
   4 uses a grammar module
   5 """
   6 
   7 import math
   8 import grammar
   9 
  10 # some lsystem constants
  11 XPOS = 0
  12 YPOS = 1
  13 ANGLE = 2
  14 DELTA = math.pi*2/3
  15 
  16 #####
  17 # The lsystem string substition rules as a dict
  18 #####
  19 
  20 RULES = {
  21     'F': 'FF',
  22     'X' : '-FXF+FXF+FXF-'
  23 }
  24 
  25 AXIOM = 'FX'
  26 
  27 ##
  28 # defining the actions as functions with a common signature
  29 ##
  30 
  31 def __turnRight(turtle, length = None):
  32     """
  33     private right turn function
  34     """
  35     turtle[ANGLE] += DELTA
  36     return turtle
  37     
  38 def __turnLeft(turtle, length = None):
  39     """
  40     private left turn function
  41     """
  42     turtle[ANGLE] -= DELTA
  43     return turtle 
  44     
  45 def __drawLine(turtle, length):
  46     """
  47     private draw line function
  48     returns a turtle at the new position
  49     """
  50     new_xpos = turtle[XPOS] + length * math.cos(turtle[ANGLE])
  51     new_ypos = turtle[YPOS] + length * math.sin(turtle[ANGLE])
  52     line(turtle[XPOS], turtle[YPOS], new_xpos, new_ypos)
  53     return [new_xpos, new_ypos, turtle[ANGLE]]
  54     
  55     
  56 lsys_op = {   # A dictionary of functions
  57     'F' : __drawLine,
  58     '-' : __turnLeft,
  59     '+' : __turnRight,
  60 }
  61 
  62 def evaluate(key, turtle, length = None):
  63     """
  64     Is a safe? wrapper controlling access to the dict of functions
  65     """
  66     if lsys_op.has_key(key):
  67         turtle = lsys_op[key](turtle, length)
  68     else:
  69         if not RULES.has_key(key): # for debugging you could comment out this line
  70             print "Unknown rule %s" % key # key is a substituted key without an action 
  71     return turtle   
  72         
  73         
  74 def render(production):        
  75     """
  76     Render evaluates the production string and calls evaluate which
  77     draws the lines etc
  78     """
  79     distance = 1.7
  80     turtle = [width/12, height/12, -DELTA]
  81     for rule in production:
  82         turtle = evaluate(rule, turtle, distance)
  83         
  84         
  85         
  86 def setup():
  87     """
  88     Is the processing setup function
  89     """
  90     size(500, 500)
  91     background(0)
  92     stroke(255)
  93     production = grammar.repeat(7, AXIOM, RULES)
  94     translate(width*0.9, height*0.9) 
  95     render(production) 
  96     
  97     
  98 def draw():
  99     """
 100     Is the processing draw loop seems to be required in processing.py 
 101     else it dies?
 102     """
 103     pass 


For the grammar module see previous postings.

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