用python tkinter显示Mandelbrot图
2015-11-01 14:08
561 查看
我前面已经讲过了用Matlab显示Mandelbrot图的方法,原理在那里也说的,链接地址:/article/9796386.html, 这次就不讲了。直接贴代码(python3):
python的计算速度肯定与Matlab是没办法比的,运行时耐心点吧。本来还绑定一个鼠标滚动放大缩小的事件,但是python计算速度太慢了,经常卡死,就放弃了。大家可以尝试一下
如何绑定鼠标滚轮的事件,请见下一篇文章。
# encoding=utf-8 from tkinter import * from random import randint def paint(LX1, LX2, LY1, LY2): xscale = float(canvas["width"]) / (LX2 - LX1) yscale = float(canvas["height"]) / (LY2 - LY1) xstep = (LX2 - LX1) / (float(canvas["width"])) ystep = (LY2 - LY1) / (float(canvas["height"])) x = LX1 while x < LX2: y = LY1 while y < LY2: c = count(complex(x, y)) if c == COUNT_LIMIT: color = "black" else: color = random_color[c-1] canvas.create_rectangle((x - LX1) * xscale, (y - LY1) * yscale, (x - LX1) * xscale, (y - LY1) * yscale, fill=color, outline = color, tags="pic") y += ystep x += xstep def count(c): z = complex(0,0) for i in range(COUNT_LIMIT): z = z*z + c if abs(z) > 2: return i return COUNT_LIMIT COUNT_LIMIT = 1000 LX1 = -2.0 LX2 = 2.0 LY1 = -2.0 LY2 = 2.0 random_color = [] for i in range(COUNT_LIMIT): r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) r = "%02x" % r g = "%02x" % g b = "%02x" % b random_color.append("#"+r+g+b) window = Tk() window.title("曼德布罗特分形") canvas = Canvas(window, width=500, height=500, bg="white") canvas.pack() paint(LX1, LX2, LY1, LY2) window.mainloop()
python的计算速度肯定与Matlab是没办法比的,运行时耐心点吧。本来还绑定一个鼠标滚动放大缩小的事件,但是python计算速度太慢了,经常卡死,就放弃了。大家可以尝试一下
如何绑定鼠标滚轮的事件,请见下一篇文章。
相关文章推荐
- [python爬虫]简单爬虫功能
- python3 抓取网页资源的 N 种方法
- Ubuntu 下为python 安装 opencv
- python os目录和文件相关操作
- python中的reduce函数
- python在Windows下将.py转换成.exe文件
- python序列化和json
- LeetCode----Remove Duplicates from Sorted List II
- python-MD5加密
- ubuntu 下python第三库 dlib 安装
- Python批量读取人脸图片与数据互相转换
- Python图片与其矩阵数据互相转换
- python-random模块
- 4.2.1 Codec基类
- Python异常(try...except)对代码运行性能的影响
- Python 通过 ACR122 修改 Mifare 1 卡数据
- python 文件读写查找、替换相关简单操作
- [LeetCode] Binary Tree Level Order Traversal
- [LeetCode] Majority Element
- python调用dll 出现[Error 193]