返回顶部
扩大
缩小

Heaton

Python基础(四) 分行树绘制demo

1 五角星绘制





"""
五角星绘制
龟库turtle
"""
import turtle

def main():
    """
        主函数
    """
    # 计数器
    count = 1

    while count <= 5:
        turtle.forward(100)
        turtle.right(144)
        count = count + 1

    turtle.exitonclick()

if __name__ == '__main__':
    main()

2 不同大小的五角星绘制




详情:https://docs.python.org/3.0/library/turtle.html

"""
不同大小五角星绘制
"""
import turtle

def draw_pentagram(size):
    """
        绘制五角星
    """
    # 计数器
    count = 1
    while count <= 5:
        turtle.forward(size)
        turtle.right(144)
        count += 1

def main():
    """
        主函数
    """
    turtle.penup()
    turtle.backward(200)
    turtle.pendown()
    turtle.pensize(2)
    turtle.pencolor('red')

    size = 40

    while size <= 200:
        # 调用函数
        draw_pentagram(size)
        size += 40

    turtle.exitonclick()

if __name__ == '__main__':
    main()

3 五角星绘制



"""
递归函数应用
"""
import turtle

def draw_recursive_pentagram(size):
    """
        迭代绘制五角星
    """
    # 计数器
    count = 1
    while count <= 5:
        turtle.forward(size)
        turtle.right(144)
        count += 1

    # 五角星绘制完成,更新参数
    size += 30
    if size <= 150:
        draw_recursive_pentagram(size)


def main():
    """
        主函数
    """
    turtle.penup()
    turtle.backward(200)
    turtle.pendown()
    turtle.pensize(2)
    turtle.pencolor('blue')

    size = 30
    draw_recursive_pentagram(size)

    turtle.exitonclick()

if __name__ == '__main__':
    main()

4 分形树绘制



"""
分形树绘制
"""
import turtle

def draw_branch(branch_length):
    """
        绘制分形树
    """
    if branch_length > 5:
        # 绘制右侧树枝
        turtle.forward(branch_length)
        print('向前 ', branch_length)
        turtle.right(20)
        print('右转 20')
        draw_branch(branch_length - 15)

        # 绘制左侧树枝
        turtle.left(40)
        print('左转 40')
        draw_branch(branch_length - 15)

        # 返回之前的树枝
        turtle.right(20)
        print('右转 20')
        turtle.backward(branch_length)
        print('向后 ', branch_length)


def main():
    """
        主函数
    """
    turtle.left(90)
    turtle.penup()
    turtle.backward(150)
    turtle.pendown()
    turtle.color('brown')
    draw_branch(80)
    turtle.exitonclick()

if __name__ == '__main__':
    main()

5 分形树绘制


加入图中代码

感谢小象

posted on 2019-02-20 16:04  咘雷扎克  阅读(509)  评论(0编辑  收藏  举报

导航