-
Notifications
You must be signed in to change notification settings - Fork 1
/
L-system-fractals.py
48 lines (39 loc) · 1003 Bytes
/
L-system-fractals.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
# Fractals using L-system
# Authour:Alan Richmond, Python3.codes
# https://en.wikipedia.org/wiki/L-system
from turtle import*
#from random import gauss
t=22 # angle of branches
d=16 # length of branches
n=4 # max depth of recursion
X="F-[[X]+X]+F[+FX]-X" # Lindenmayer system
F="F"
stack=[]
def x(n):
if n>0: L(X,n)
else: dot(16,'green')
def f(n):
if n>0: L(F,n)
def L(s,n):
pensize(n*2)
for c in s:
# if c=='-': lt(gauss(t,t))
# elif c=='+': rt(gauss(t,t))
if c=='-': lt(t)
elif c=='+': rt(t)
elif c=='X': x(n-1)
elif c=='F': f(n-1);fd(d)
elif c=='[': stack.append((pos(),heading(),n))
elif c==']':
((i,j),h,p)=stack.pop()
penup()
goto(i,j)
seth(h)
pensize(p)
pendown()
penup()
goto(0,-200)
pendown()
seth(90)
color('brown','green')
x(n)