Skip to content

Instantly share code, notes, and snippets.

Created October 25, 2015 09:03
Show Gist options
  • Save anonymous/31db7fdeb9f0226cbf67 to your computer and use it in GitHub Desktop.
Save anonymous/31db7fdeb9f0226cbf67 to your computer and use it in GitHub Desktop.
test
"""
"F": Move forward a step of length d. A line segment between
points (X,Y,Z) and (X',Y',Z') is drawn.
"["
and
"]": bracket - push and pop the current state, in this project
it is used to generate the tree branches
"+": Turn left by angle Delta, Using rotation matrix R_U(Delta)
"-": Turn right by angle Delta, Using rotation matrix R_U(-Delta)
"&": Pitch down by angle Delta, Using rotation matrix R_L(Delta)
"+": Pitch up by angle Delta, Using rotation matrix R_L(-Delta)
"<": Roll left by angle Delta, Using rotation matrix R_H(Delta)
">": Roll right by angle Delta, Using rotation matrix R_H(-Delta)
"|": Turn around, Using rotation matrix R_H(180)
"""
class Vector:
def __init__(self, axis):
self.x = axis[0]
self.y = axis[1]
self.z = axis[2]
return
def produce(axiom, rules):
output = ""
for i in axiom:
output = output + rules.get(i, i)
return output
def iterate(n, axiom, rules):
# print(axiom)
if n > 0:
axiom = produce(axiom, rules)
return iterate(n - 1, axiom, rules)
return axiom
class Lturtle:
stackstate = [] # remembers saved state
delta = 0 # angle of rotation
length = 0.5 # full length of turtle move
thickness = 0.02 # default thickness of cylinder
instrudict = {
'+': 'turnleft',
'-': 'turnright',
'&': 'pitchdown',
'^': 'pitchup',
'<': 'leftroll',
'>': 'rightroll',
'|': 'turn180',
'%': 'roll180',
'$': 'rollhoriz',
'x': 'randturn',
't': 'gravity',
'F': 'fdraw',
'f': 'fnodraw',
'Z': 'halfdraw',
'z': 'halfnodraw',
'g': 'Fnorecord',
'.': 'Nomove'
}
def __init__(self, vPos=Vector((0, 0, 0)), vH=Vector((0, 0, 1)), vL=Vector((0, 1, 0)), vU=Vector((1, 0, 0))):
"""The turtle will start at the origin with the
Z-axis as forward direction and Y-axis as left
direction. (lparser.txt)"""
self.vPos, self.vH, self.vL, self.vU = vPos, vH, vL, vU
def chomp(self, instructions):
getparam = 0
checkparam = 0
param = ""
for item in instructions:
if getparam:
if item == ")":
getparam = 0 # done getting
command = command + "(" + param + ")"
eval(command)
continue
else:
param = param + item # building parameter
continue
if checkparam: # checking for parameter?
checkparam = 0
if item == "(":
param = ""
getparam = 1 # parameter exists
continue
else:
command = command + "()" # no parameter
eval(command)
# initializing command string
command = "self." + self.instrudict.get(item)
checkparam = 1 # set flag
else: # dealing with last item
if checkparam:
command = command + "()" # no parameter
eval(command)
def fnodraw(self, n=""):
print("Forward %s (no draw)" % n)
def turnleft(self, n=""):
print("Turn Left around vU %s" % n)
def turnright(self, n=""):
print("Turn Right around vU %s" % n)
""" USAGE
http://www.4dsolutions.net/ocn/lsystems.html
axiom = 'F--F--F'
rules = {}
rules['F'] = 'F+F--F+F'
newaxiom = produce(axiom, rules)
f = produce(newaxiom, rules)
print(f)
rules = {}
rules['A'] = 'B'
rules['B'] = 'AB'
axiom = 'A'
iterate(7, axiom, rules)
>>> rules = {}
>>> rules['I']='+(40)ffHccI'
>>> rules['H']='[+fffG]c[-fffG]c[^fffG]c[&fffG]G'
>>> rules['G']='[dS]e[d]e[d]e[d]e[d]e[d]e[d]e[d]'
>>> rules['e']='+(90)f-(90)>(-45)'
>>> rules['d']=('{[f+(90)f+(90)f+(90)f>(+38)-(105)ff-(151)ff]'
'[f&(38)+(15)ff+(151)ff]}')
>>> axiom = 'I'
>>> iterate(2,axiom,rules)
I
+(40)ffHccI
+(40)ff[+fffG]c[-fffG]c[^fffG]c[&fffG]Gcc+(40)ffHccI
"""
rules = {}
rules['I'] = '+(40)ffHccI'
rules['H'] = '[+fffG]c[-fffG]c[^fffG]c[&fffG]G'
rules['G'] = '[dS]e[d]e[d]e[d]e[d]e[d]e[d]e[d]'
rules['e'] = '+(90)f-(90)>(-45)'
rules['d'] = '{[f+(90)f+(90)f+(90)f>(+38)-(105)ff-(151)ff][f&(38)+(15)ff+(151)ff]}'
axiom = 'I'
m = iterate(2, axiom, rules)
print(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment