-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlsystem25.py
123 lines (105 loc) · 2.95 KB
/
lsystem25.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
# Time : 2019/4/6 21:20
# Author : Mifen
# Email : [email protected]
# Github : https://github.com/Amd794
import turtle as t
import random
def draw_path(length, angle, path, expalnation, lengthFactor):
posList, angleList = [], []
for symbol in path:
# print(expalnation[symbol])
if symbol == 'F':
t.color(getColor(), getColor(), getColor())
t.forward(length)
elif symbol == '>':
length = length * lengthFactor
elif symbol == '<':
length = length / lengthFactor
elif symbol == '+':
t.left(angle)
elif symbol == '-':
t.rt(angle)
elif symbol == '[':
posList.append(t.pos())
angleList.append(t.heading())
elif symbol == 'a':
t.pensize(4)
t.color("green")
elif symbol == 'b':
t.color("green")
t.pensize(3)
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(path, rules):
L = [_ for _ in path]
for i in range(len(L)):
symbol = L[i]
characters = 'Fa'
for character in characters:
if symbol == character:
try:
L[i] = rules[symbol]
except KeyError:
continue
path = ''.join(L)
return path
def getColor():
t.colormode(255)
return random.randint(0, 255)
def Introduction(x=-600, y=-350):
t.up()
t.color(getColor(), getColor(), getColor())
t.goto(-600, 300)
t.write('Author:Mifen', 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.color("black")
t.goto(x, y)
t.down()
def initialization():
t.setup(1280, 720)
t.bgcolor("grey")
t.speed(0)
t.pensize(1)
def run(n, angle, length, path, rules, lengthFactor):
initialization()
Introduction(200, -300)
expalnation = {
'F': '画线',
'x': '-',
'+': '逆时针旋转',
'-': '顺时针旋转',
'[': '记录当前位置',
']': '恢复上一个位置',
'a': '上色',
'b': '上色',
'c': '上色',
'>': '伸长',
'<': '缩短',
'y': '-'
}
for _ in range(n):
path = apply_rules(path, rules)
print(path)
draw_path(length, angle, path, expalnation, lengthFactor)
t.done()
if __name__ == '__main__':
angle = 35
length = 15
lengthFactor = 1.36
path = 'F' # 初始路径
rules = {
'F': 'F[+FF][-FF]F[-F][+F]F' # 转换规则
}
t.lt(90)
run(5, angle, length, path, rules, lengthFactor)