利用Python画小树和森林(宽度优先绘制+深度优先绘制)

在中M上看嵩天老师的《Python语言程序设计》时,视频中给出了宽度优先绘制法的代码,仅提了一下还有另外一种画法(深度优先绘制法).联系到最近学的数据结构,摸索出了另一种画法。

源码:

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
'''
Created on Dec 2, 2017

@author: QiZhao
'''
# drawtree.py

from turtle import Turtle

def tree(plist, l, a, f):
#宽度优先绘制法
""" plist is list of pens
l is length of branch
a is half of the angle between 2 branches
f is factor by which branch is shortened
from level to level."""
if l > 5:
lst = []
for p in plist:
p.forward(l)#Move the turtle forward by the specified distance, in the direction the turtle is headed.
q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
p.left(a) #Turn turtle left by angle units
q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
lst.append(p)
lst.append(q)
tree(lst, l*f, a, f)

def tree2(p,l,a,f):
#深度优先绘制法
if l>5:
p.forward(l)
q=p.clone()
p.left(a)
q.right(a)
tree2(p, l*f, a, f)
tree2(q, l*f, a, f)

def maketree(x,y):
p = Turtle()
p.color("blue")
p.pensize(5)
p.setundobuffer(None)
p.hideturtle()
#Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,
#because hiding the turtle speeds up the drawing observably.
#p.speed(9)
p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
#TurtleScreen methods can then be called for that object.
p.left(90)# Turn turtle left by angle units. direction

p.penup() #Pull the pen up – no drawing when moving.
p.goto(x,y)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
p.pendown()# Pull the pen down – drawing when moving.
#这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画.否则turtle一移动就会自动的把线画出来

tree([p], 100, 65, 0.6375)

p.penup()
p.setheading(90)#Set the orientation of the turtle to to_angle.
p.goto(x,y)
p.down()
p.color("green")

tree2(p, 100, 65, 0.6375)

def main():
maketree(-200, -200)
maketree(0, 0)
maketree(200,-200)

main()

效果图

您的支持是我继续创作最大的动力!

欢迎关注我的其它发布渠道