Last active
July 18, 2021 20:09
-
-
Save HansNewbie/9808789 to your computer and use it in GitHub Desktop.
Python Turtle Fractal Plant
This file contains 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
# As described in http://en.wikipedia.org/wiki/L-system#Example_7:_Fractal_plant | |
import turtle | |
ruleInput = ['F', 'X'] | |
ruleOutput = ["FF", "F-[[X]+X]+F[+FX]-X"] | |
start = "X" | |
front = 5 | |
turn = 30 | |
stack = [] | |
dirstack = [] | |
turtle.left(90) | |
turtle.penup() | |
turtle.setpos(0, -350) | |
turtle.pendown() | |
turtle.shape("turtle") | |
def generate(iteration): | |
result = start | |
temp = "" | |
for i in range(iteration): | |
for j in range(len(result)): | |
for k in range(len(ruleInput)): | |
if (result[j] == ruleInput[k]): | |
temp += ruleOutput[k] | |
break | |
if (k == len(ruleInput)-1): | |
temp += result[j] | |
result = temp | |
temp = "" | |
return result | |
def draw(input): | |
for x in input: | |
if (x == 'F'): | |
turtle.forward(front) | |
elif (x == '-'): | |
turtle.left(turn) | |
elif (x == '+'): | |
turtle.right(turn) | |
elif (x == '['): | |
stack.append(turtle.pos()) | |
dirstack.append(turtle.heading()) | |
elif (x == ']'): | |
turtle.penup() | |
post = stack.pop() | |
direc = dirstack.pop() | |
turtle.setpos(post) | |
turtle.setheading(direc) | |
turtle.pendown() | |
turtle.hideturtle() | |
turtle.done() | |
draw(generate(6)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment