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


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


源码:

'''
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()


效果图:

全部评论

相关推荐

程序员小白条:可以,技术栈别写太多,因为学院本这块,没必要太多,项目的话可以提前,技术栈放最下面,要么技术栈放最前面,多准备下八股文
点赞 评论 收藏
分享
老板加个卤鸡蛋:HR看了以为来卧底来了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 一张图晒出你司的标语 #
4337次浏览 75人参与
# AI面会问哪些问题? #
27981次浏览 558人参与
# 厦门银行科技岗值不值得投 #
8044次浏览 188人参与
# 你的实习产出是真实的还是包装的? #
20238次浏览 342人参与
# 找AI工作可以去哪些公司? #
9200次浏览 237人参与
# 春招至今,你的战绩如何? #
65485次浏览 583人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
15262次浏览 221人参与
# 从事AI岗需要掌握哪些技术栈? #
9021次浏览 309人参与
# 中国电信笔试 #
32017次浏览 292人参与
# 你做过最难的笔试是哪家公司 #
33703次浏览 237人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
340862次浏览 2175人参与
# 哪些公司真双非友好? #
69618次浏览 289人参与
# 阿里笔试 #
178666次浏览 1317人参与
# 机械人避雷的岗位/公司 #
62704次浏览 393人参与
# 小马智行求职进展汇总 #
25133次浏览 80人参与
# 第一份工作一定要去大厂吗 #
14702次浏览 122人参与
# 金三银四,你的春招进行到哪个阶段了? #
22097次浏览 280人参与
# 为了减少AI幻觉,你注入过哪些设定? #
26258次浏览 310人参与
# 应届生第一份工资要多少合适 #
20690次浏览 86人参与
# 沪漂/北漂你觉得哪个更苦? #
9917次浏览 193人参与
# 聊聊你的职场新体验 #
336513次浏览 1895人参与
# HR最不可信的一句话是__ #
6300次浏览 114人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务