-
Notifications
You must be signed in to change notification settings - Fork 0
/
LindIter.py
152 lines (121 loc) · 5.71 KB
/
LindIter.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 1 18:34:44 2020
@author: Roneet
"""
def LindIter(System, N):
# LINDITER Returns a Lindenmayer string after calculating N iterations for
# it.
#
# Usage: LindenmayerString = LindIter(System, N)
#
# Takes the name of the system as a string, and the number of
# iterations as an int, as arguments. The function LindIter contains
# nested functions for each of the Lindenmayer system types, which in turn
# calculate a single iteration according to the L-system types' individual
# configurations. After running N iterations on the initial string
# (different for each L-system), the final string is returned.
#
# Author: Roneet V. Nagale, [email protected], 2020
LindenmayerString = "Default."
# Each of the following nested functions add one iteration of the
# L-string to the input string and return it. They are called in a
# for loop and are passed the respective initial string for each their
# L-system type. They then
# Convert the intial string for Koch curves (S) to an L-string.
def KochConvert(String):
#Split the string into an array.
arrayString = list(String)
outputString = ''
#Replace characters according to L-system rules.
for character in arrayString:
if character == 'S':
indexS = arrayString.index('S')
arrayString[indexS] = 'SLSRSLS'
elif character == 'L':
indexL = arrayString.index('L')
arrayString[indexL] = 'L'
elif character == 'R':
indexR = arrayString.index('R')
arrayString[indexR] = 'R'
# Join the array after the loop finishes, and return the string.
outputString = outputString.join(arrayString)
KochString = outputString
return KochString
# Convert the initial string for Sierpinski Triangles (A) to an L-string
def SierpinskiConvert(String):
#Split the string into an array.
arrayString = list(String)
outputString = ''
#Replace characters according to L-system rules.
for character in arrayString:
if character == 'A':
indexA = arrayString.index('A')
arrayString[indexA] = 'BRARB'
elif character == 'B':
indexB = arrayString.index('B')
arrayString[indexB] = 'ALBLA'
elif character == 'L':
indexL = arrayString.index('L')
arrayString[indexL] = 'L'
elif character == 'R':
indexR = arrayString.index('R')
arrayString[indexR] = 'R'
# Join the array after the loop finishes, and return the string.
outputString = outputString.join(arrayString)
SierpinskiString = outputString
return SierpinskiString
# Convert the initial string for Dragon Curves (FX) to an L-string.
def DragonCurveConvert(String):
#Split the string into an array.
arrayString = list(String)
outputString = ''
#Replace characters according to L-system rules.
for character in arrayString:
if character == 'X':
indexA = arrayString.index('X')
arrayString[indexA] = 'XRYFR'
elif character == 'Y':
indexB = arrayString.index('Y')
arrayString[indexB] = 'LFXLY'
elif character == 'L':
indexL = arrayString.index('L')
arrayString[indexL] = 'L'
elif character == 'R':
indexR = arrayString.index('R')
arrayString[indexR] = 'R'
# Join the array after the loop finishes, and return the string.
outputString = outputString.join(arrayString)
DragonCurveString = outputString
return DragonCurveString
# Check which L-system was received as an argument.
# If the argument is valid, set a variable to the initial string for the
# L-system. Pass the variable to the relevant nested function N
# times, saving the output in itself each time.
#
# Assign the calculated L-string to the LindenmayerString variable,
# which is returned at the end of the LindIter function.
# If an invalid argument is revieved, print an error-message to the user
# and exit the function, returning to the main menu.
if System == "Koch":
KochString = 'S'
for n in range(0,N):
KochString = KochConvert(KochString)
LindenmayerString = KochString
elif System == "Sierpinski":
SierpinskiString = 'A'
for n in range(0,N):
SierpinskiString = SierpinskiConvert(SierpinskiString)
LindenmayerString = SierpinskiString
elif System == "Dragon Curve":
DragonCurveString = 'FX'
for n in range(0, N):
DragonCurveString = DragonCurveConvert(DragonCurveString)
LindenmayerString = DragonCurveString
else:
print("Error! No valid system or number found.")
return LindenmayerString
# Test code to check if the LindIter function behaves as expected and returns
# the correct string for each system.
assert LindIter("Koch", 2) == 'SLSRSLSLSLSRSLSRSLSRSLSLSLSRSLS'
assert LindIter("Sierpinski", 2) == 'ALBLARBRARBRALBLA'