Created
July 28, 2010 23:42
-
-
Save cmaggard/496759 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
def representation(self, generation=1, rep=None): | |
"""Returns the L-system string after _x_ generations""" | |
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 | |
def base_rule(self): | |
"""Returns a string that describes the base rule for the L-system""" | |
raise NotImplementedError | |
def turn_radius(self): | |
"""The amount that a right or left turn instruction should turn""" | |
raise NotImplementedError | |
def length_multiplier(self, gen): | |
return 1 | |
class KochSnowflakeSystem(System): | |
"""Example L-system derived from System that highlights basic behaviour""" | |
def base_rule(self): | |
return "F++F++F" | |
def turn_radius(self): | |
return 60 | |
def rule_f(self): | |
return "F-F++F-F" | |
def length_multiplier(self, gen): | |
return 1.0 / (3 ** (gen - 1)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment