-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractals.py
44 lines (30 loc) · 840 Bytes
/
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
import turtle
from math import sqrt
def rule_applying(axiom, rules, num_iter):
frac_instr = axiom
for _ in range(num_iter):
for ch in rules:
frac_instr = frac_instr.replace(ch, rules[ch])
return frac_instr
def instr_applying(turt, instr, angle, forw_len):
if instr == "F":
turt.forward(forw_len)
elif instr == "+":
turt.left(angle)
elif instr == "-":
turt.right(angle)
axiom = 'F'
rules = {'F': 'F+F---FFF+++F-F'}
angle = 45
forw_len = 40
win = turtle.Screen()
win.setup(width=800, height=600)
curs = turtle.Turtle()
curs.hideturtle()
turtle.tracer(0, 0)
curs.color('white')
curs.goto(330, 30)
curs.color('black')
frac_instructions = rule_applying(axiom, rules, 2)
for instruction in frac_instructions:
instr_applying(curs, instruction, angle, forw_len)