今天是植树节,你“植树”了吗?

举报
爱打瞌睡的CV君 发表于 2022/07/08 00:04:42 2022/07/08
【摘要】 今天是植树节,不要忘记植树嗷! 文章目录 一、代码二、运算结果 一、代码 # -*- coding: UTF-8 -*- """ @Author :远方的星 @Time : 20...

今天是植树节,不要忘记植树嗷!

一、代码

# -*- coding: UTF-8 -*-
"""
@Author  :远方的星
@Time   : 2021/3/2 14:28
@CSDN    :https://blog.csdn.net/qq_44921056
@腾讯云   : https://cloud.tencent.com/developer/column/91164
"""

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

  
 

二、运算结果

在这里插入图片描述

文章来源: luckystar.blog.csdn.net,作者:爱打瞌睡的CV君,版权归原作者所有,如需转载,请联系作者。

原文链接:luckystar.blog.csdn.net/article/details/114685680

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。