渐入佳境。

# coding: utf-8

import turtle

'''
# =================turtle练手==
def draw_spiral(my_turtle, line_len):
    if line_len > 0:
        my_turtle.forward(line_len)
        my_turtle.right(70)
        draw_spiral(my_turtle, line_len-10)

my_tur = turtle.Turtle()
my_win = turtle.Screen()
draw_spiral(my_tur, 200)
my_win.exitonclick()

# =================分形树==
def tree(branch_len, my_turtle):
    if branch_len > 5:
        my_turtle.forward(branch_len)
        my_turtle.right(20)
        tree(branch_len-15, my_turtle)
        my_turtle.left(40)
        tree(branch_len - 15, my_turtle)
        my_turtle.right(20)
        my_turtle.backward(branch_len)

my_tur = turtle.Turtle()
my_win = turtle.Screen()

my_tur.left(90)
my_tur.up()
my_tur.backward(100)
my_tur.down()
my_tur.color('green')
tree(100, my_tur)
my_win.exitonclick()

# =================谢尔宾斯基三角形==
def draw_triangle(points, color, my_turtle):
    my_turtle.fillcolor(color)
    my_turtle.up()
    my_turtle.goto(points[0][0], points[0][1])
    my_turtle.down()
    my_turtle.begin_fill()
    my_turtle.goto(points[1][0], points[1][1])
    my_turtle.goto(points[2][0], points[2][1])
    my_turtle.goto(points[0][0], points[0][1])
    my_turtle.end_fill()

def get_mid(p1, p2):
    return (p1[0]+p2[0])/2, (p1[1]+p2[1])/2

def sierpinski(points, degree, my_turtle):
    color_map = ['blue', 'red', 'green', 'white',
                 'yellow', 'violet', 'orange']
    draw_triangle(points, color_map[degree], my_turtle)
    if degree > 0:
        sierpinski([points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])], degree-1, my_turtle)
        sierpinski([points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])], degree-1, my_turtle)
        sierpinski([points[2], get_mid(points[2], points[1]), get_mid(points[0], points[2])], degree-1, my_turtle)

my_tur = turtle.Turtle()
my_win = turtle.Screen()
my_points = [[-100, -50], [0, 100], [100, -50]]
sierpinski(my_points, 4, my_tur)
my_win.exitonclick()
'''

# =======================================汉诺塔==
def move_tower(height, from_pole, to_pole, with_pole):
    if height >= 1:
        move_tower(height-1, from_pole, with_pole, to_pole)
        move_disk(from_pole, to_pole, height)
        move_tower(height-1, with_pole, to_pole, from_pole)

def move_disk(fp, tp, disk_name):
    print('moving 碟子 {0} from  {1} to {2}'.format(disk_name, fp, tp))

high = 4

move_tower(high, '起始杆', '末尾杆', '中间杆')

  

C:\Users\Sahara\.virtualenvs\untitled\Scripts\python.exe D:/test/python_recursion.py
moving 碟子 1 from  起始杆 to 中间杆
moving 碟子 2 from  起始杆 to 末尾杆
moving 碟子 1 from  中间杆 to 末尾杆
moving 碟子 3 from  起始杆 to 中间杆
moving 碟子 1 from  末尾杆 to 起始杆
moving 碟子 2 from  末尾杆 to 中间杆
moving 碟子 1 from  起始杆 to 中间杆
moving 碟子 4 from  起始杆 to 末尾杆
moving 碟子 1 from  中间杆 to 末尾杆
moving 碟子 2 from  中间杆 to 起始杆
moving 碟子 1 from  末尾杆 to 起始杆
moving 碟子 3 from  中间杆 to 末尾杆
moving 碟子 1 from  起始杆 to 中间杆
moving 碟子 2 from  起始杆 to 末尾杆
moving 碟子 1 from  中间杆 to 末尾杆

Process finished with exit code 0

  

python---使用递归实现谢尔宾斯基三角形及汉诺塔的更多相关文章

  1. Python使用递归绘制谢尔宾斯基三角形

    谢尔宾斯基三角形使用了三路递归算法,从一个大三角形开始,通过连接每一个边的中点,将大三角型分为四个三角形,然后忽略中间的三角形,依次对其余三个三角形执行上述操作. 运行效果: 源代码: 1 impor ...

  2. 【数据结构与算法Python版学习笔记】递归(Recursion)——定义及应用:分形树、谢尔宾斯基三角、汉诺塔、迷宫

    定义 递归是一种解决问题的方法,它把一个问题分解为越来越小的子问题,直到问题的规模小到可以被很简单直接解决. 通常为了达到分解问题的效果,递归过程中要引入一个调用自身的函数. 举例 数列求和 def ...

  3. python 使用turtule绘制递归图形(螺旋、二叉树、谢尔宾斯基三角形)

    插图工具使用Python内置的turtle模块,为什么叫这个turtle乌龟这个名字呢,可以这样理解,创建一个乌龟,乌龟能前进.后退.左转.右转,乌龟的尾巴朝下,它移动时就会画一条线.并且为了增加乌龟 ...

  4. 小练手:用HTML5 Canvas绘制谢尔宾斯基三角形

    文章首发于我的知乎专栏,原地址:https://zhuanlan.zhihu.com/p/26606208 以前看到过一个问题:谢尔宾斯基三角形能用编程写出来么?该怎么写? - 知乎,在回答里,各方大 ...

  5. 分形之谢尔宾斯基(Sierpinski)三角形

    谢尔宾斯基三角形(英语:Sierpinski triangle)是一种分形,由波兰数学家谢尔宾斯基在1915年提出,它是一种典型的自相似集.也有的资料将其称之为谢尔宾斯基坟垛. 其生成过程为: 取一个 ...

  6. 分形之谢尔宾斯基(Sierpinski)地毯

    前面讲了谢尔宾斯基三角形,和这一节的将把三角形变为正方形,即谢尔宾斯基地毯,它是由瓦茨瓦夫·谢尔宾斯基于1916年提出的一种分形,是自相似集的一种. 谢尔宾斯基地毯的构造与谢尔宾斯基三角形相似,区别仅 ...

  7. 分形之谢尔宾斯基(Sierpinski)四面体

    前面讲了谢尔宾斯基三角形,这一节的将对二维三角形扩展到三维,变成四面体.即将一个正四面体不停地拆分,每个正四面体可以拆分成四个小号的正四面体.由二维转变到三维实现起来麻烦了许多.三维的谢尔宾斯基四面体 ...

  8. 混沌分形之谢尔宾斯基(Sierpinski)

    本文以使用混沌方法生成若干种谢尔宾斯基相关的分形图形. (1)谢尔宾斯基三角形 给三角形的3个顶点,和一个当前点,然后以以下的方式进行迭代处理: a.随机选择三角形的某一个顶点,计算出它与当前点的中点 ...

  9. 【python】递归(阶乘、斐波纳契、汉诺塔)

随机推荐

  1. C++ 读取字符串中的数字

    今天真是试了各种方法,笨方法聪明方法都有了 方法1:一个字符一个字符的读取 方法2:借助strtok实现split 适用于char 方法3:借助istringstream实现split 适用于stri ...

  2. C# 操作文件类,文件夹存在判断,创建,文件内容读写。

    此篇文档是老早就放到草稿箱中了,断断续续编辑了几次.结合自己经常用到的对于文件的操作,结合msdn做此文. 1.File 类 --转自msdn 常用方法: 名称 说明 AppendAllLines(S ...

  3. shell中的dd命令使用详解

    一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...

  4. percona mysql5.7关闭审计功能方法

    数据库的审计日志占用大量空间,当时是为了测试审计功能开启的,现在需要关闭 # /data/mysql_data]# du -sh * 124G audit.log # 查询审计相关参数 mysql&g ...

  5. Golang -- Signal处理

    我们在生产环境下运行的系统要求优雅退出,即程序接收退出通知后,会有机会先执行一段清理代码,将收尾工作做完后再真正退出.我们采用系统Signal来 通知系统退出,即kill pragram-pid.我们 ...

  6. Eclipse中三种设置编码格式的方法

    转自:https://blog.csdn.net/rainy_black_dog/article/details/52403735 很早以前听过一位老师说过:咱们中国人不管学习哪种编程语言,总会遇到乱 ...

  7. 解决layui选中项下一页清空问题

    项目中遇到给用户在所有产品中匹配一部分产品.用layui 第一页选好之后到第二页再选,等回到第一页时之前选择的都没了,解决这个问题的办法如下: //勾选的产品id集合 var chooseAdids ...

  8. ES的Query、Filter、Metric、Bucketing使用详解

    由于笔者在实际项目仅仅将ES用作索引数据库,并没有深入研究过ES的搜索功能.而且鉴于笔者的搜索引擎知识有限,本文将仅仅介绍ES简单(非全文)的查询API. 笔者原本打算在本文中介绍聚合API的内容,但 ...

  9. liux三剑客grep 正则匹配

    001正则匹配(大部分需要转义) ‘^‘: 锚定行首 '$' : 锚定行尾 [0-9] 一个数字 [^0-9] 除去数字所有,^出现在[]这里表示取反 [a-z] [A-Z] [a-Z] \s 匹配空 ...

  10. 【进阶4-2期】Object.assign 原理及其实现 (转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记  https://github.com/yygmind/blog/issues/26 浅拷贝 Object.assign 上篇文章介绍了其定义和使 ...