Skip to content

Commit

Permalink
refactored l-system code
Browse files Browse the repository at this point in the history
  • Loading branch information
schellenberg committed Jan 6, 2025
1 parent 2a4b005 commit dc7aca1
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions _sources/Python/Strings/LSystems.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,29 @@ above.
"""Apply rules to an individual letter, and return the result."""
# Rule 1
if letter == 'A':
new_string = 'B'
return 'B'

# Rule 2
elif letter == 'B':
new_string = 'AB'
return 'AB'

# no rules apply so keep the character
else:
new_string = letter

return new_string
return letter

def process_string(original_string):
"""Apply rules to a string, one letter at a time, and return the result."""
tranformed_string = ""
for letter in original_string:
tranformed_string = tranformed_string + apply_rules(letter)

return tranformed_string

def create_l_system(number_of_iterations, axiom):
"""Begin with an axiom, and apply rules to the original axiom string number_of_iterations times, then return the result."""
start_string = axiom
new_string = axiom
for counter in range(number_of_iterations):
end_string = process_string(start_string)
start_string = end_string

return end_string
new_string = process_string(new_string)
return new_string

print(create_l_system(4, "A"))

Expand Down Expand Up @@ -208,13 +203,12 @@ Here is the ``apply_rules`` function for this L-system.
"""Apply rules to an individual letter, and return the result."""
# Rule 1
if letter == 'F':
new_string = 'F-F++F-F'
return 'F-F++F-F'

# no rules apply so keep the character
else:
new_string = letter
return letter

return new_string

Pretty simple so far. As you can imagine this string will get pretty long
with a few applications of the rules. You might try to expand the string a
Expand Down Expand Up @@ -259,30 +253,25 @@ Here is the complete program, which combines generating the L-System string, and
"""Apply rules to an individual letter, and return the result."""
# Rule 1
if letter == 'F':
new_string = 'F-F++F-F'
return 'F-F++F-F'

# no rules apply so keep the character
else:
new_string = letter

return new_string
return letter

def process_string(original_string):
"""Apply rules to a string, one letter at a time, and return the result."""
tranformed_string = ""
for letter in original_string:
tranformed_string = tranformed_string + apply_rules(letter)

return tranformed_string

def create_l_system(number_of_iterations, axiom):
"""Begin with an axiom, and apply rules to the original axiom string number_of_iterations times, then return the result."""
start_string = axiom
new_string = axiom
for counter in range(number_of_iterations):
end_string = process_string(start_string)
start_string = end_string

return end_string
new_string = process_string(new_string)
return new_string

def draw_l_system(some_turtle, instructions, angle, distance):
"""Draw with some_turtle, interpreting each letter in the instructions passed in."""
Expand Down

0 comments on commit dc7aca1

Please sign in to comment.