-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBaseLsystem.py
101 lines (92 loc) · 2.75 KB
/
BaseLsystem.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
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# Time : 2020/12/12 15:25
# Author : Amd794
# Email : [email protected]
# Github : https://github.com/Amd794
# -*- coding: utf-8 -*-
# Time : 2019/4/5 22:20
# Author : Mifen
# Email : [email protected]
# Github : https://github.com/Amd794
import turtle as t
class Pic(object):
def __init__(self,
init_path: str,
rules: dict,
angle: int,
length: int = 5,
deep: int = 5) -> None:
t.setup(1280, 720)
t.speed(0)
t.pensize(1)
self.deep = deep
self.about_me()
self.length = length
self.init_path = init_path
self.angle = angle
self.rules = rules
def about_me(self):
t.up()
t.color("#262626")
t.goto(-600, 300)
t.write('Author:Amd794', font=("微软雅黑", 18))
t.goto(-600, 250)
t.write('E-mail :[email protected]', font=("微软雅黑", 18))
t.goto(-600, 200)
t.write('Code :https://github.com/Amd794/Python123', font=("微软雅黑", 18))
t.goto(-600, -350)
t.down()
def draw_path(self, apply_path):
posList, angleList = [], []
t.up()
t.goto(0, -350)
t.down()
t.lt(90)
for symbol in apply_path:
if symbol == 'F':
t.forward(self.length)
elif symbol == '+':
t.left(self.angle)
elif symbol == '-':
t.rt(self.angle)
elif symbol == '[':
posList.append(t.pos())
angleList.append(t.heading())
elif symbol == 'a':
t.pensize(3)
t.color("#8c503c")
elif symbol == 'b':
t.pensize(2)
t.color("#4ab441")
elif symbol == 'c':
t.pensize(2)
t.color("#18b418")
elif symbol == ']':
t.up()
t.home()
t.goto(posList.pop())
t.left(angleList.pop())
t.down()
def apply_rules(self):
apply_path = self.init_path
for k in range(self.deep):
L = [_ for _ in apply_path]
for i in range(len(L)):
symbol = L[i]
if symbol == 'F':
L[i] = self.rules[symbol]
if symbol == 'X':
L[i] = self.rules[symbol]
apply_path = ''.join(L)
return apply_path
if __name__ == '__main__':
rules = {
'F': 'aFF-[b-F+F]+[c+F-F]',
'X': 'aFF+[b+F]+[c-F]'
}
angle = 25
init_path = 'FX'
p = Pic(init_path, rules, angle)
apply_path = p.apply_rules()
p.draw_path(apply_path)