Skip to content

Instantly share code, notes, and snippets.

@cmaggard
Created July 26, 2010 00:34
Show Gist options
  • Save cmaggard/490029 to your computer and use it in GitHub Desktop.
Save cmaggard/490029 to your computer and use it in GitHub Desktop.
import re
class System(object):
matcher = re.compile("^rule_")
def __init__(self):
self.base = self.base_rule()
self.__introspect()
def __introspect(self):
funcs = {re.sub(System.matcher, '', x).upper(): self.__getattribute__(x) for x
in dir(self) if System.matcher.match(x)}
self.funcs = funcs
"""Returns the L-system string after _x_ generations"""
def representation(self, generation=1, rep=None):
if rep is None:
rep = self.base
if generation is 1:
return rep
else:
rep = self.process(rep)
return self.representation(generation-1, rep)
def process(self, rep):
return "".join([self.process_char(x) for x in rep])
def process_char(self, char):
return self.funcs[char]() if self.funcs.has_key(char) else char
"""Returns a string that describes the base rule for the L-system"""
def base_rule(self):
raise NotImplementedError
# """The amount that a right or left turn instruction should turn"""
# def turn_radius(self):
# raise NotImplementedError
#
# """'+' instruction means to turn right"""
# def plus(self):
# return lambda x: x.right(self.turn_radius)
# pass
#
# def minus(self):
# return lambda x: x.left(self.turn_radius)
# pass
"""Example L-system derived from System that highlights basic behaviour"""
class KochSnowflakeSystem(System):
def base_rule(self):
return "F++F++F"
def turn_radius(self):
return 60
def rule_f(self):
return "F-F++F-F"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment