-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsys.py
65 lines (57 loc) · 1.73 KB
/
lsys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
######################################
# lsys.py
# General lindenmeyer system functions
# author: stphndemos
######################################
from random import random
class lsystem():
alphabet = {}
axiom = ''
rules = {}
"""
params:
alphabet:
dictionary of possible characters as keys,
with function to perform as value
axiom:
the base string (depth 0)
rules:
dictionary of production rules to apply
recursively, depth times
"""
def __init__(self, alphabet, axiom, rules):
self.alphabet = alphabet
self.axiom = axiom
self.rules = rules
"""
params:
depth:
how many times to recursively apply produciton rules
tree:
the base tree, defaults to the provided axiom
returns:
tree with rules applied depth times
"""
def apply_rules(self, depth, tree = 'workaround!', random_weight = .5):
if tree == 'workaround!':
tree = self.axiom
if depth == 0:
return tree
out_tree = ''
for c in tree:
if c in self.rules:
if c.lower() in self.rules:
if random() > random_weight:
c = c.lower()
out_tree += self.rules[c]
else:
out_tree += c
return self.apply_rules(depth-1, out_tree)
def perform_actions(self, tree = axiom):
for c in tree:
self.alphabet[c]()
def gen_lsys(alphabet, axiom, rules, depth, rw = .5):
system = lsystem(alphabet, axiom, rules)
output = system.apply_rules(depth, random_weight = rw)
print output
system.perform_actions(output)