用python tkinter显示Mandelbrot图
生活随笔
收集整理的這篇文章主要介紹了
用python tkinter显示Mandelbrot图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我前面已經講過了用Matlab顯示Mandelbrot圖的方法,原理在那里也說的,鏈接地址:http://blog.csdn.net/whoispo/article/details/49557823, 這次就不講了。直接貼代碼(python3):
# encoding=utf-8from tkinter import * from random import randintdef 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 = LX1while x < LX2:y = LY1while 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 += ystepx += xstepdef count(c):z = complex(0,0)for i in range(COUNT_LIMIT):z = z*z + cif abs(z) > 2: return ireturn COUNT_LIMITCOUNT_LIMIT = 1000 LX1 = -2.0 LX2 = 2.0 LY1 = -2.0 LY2 = 2.0random_color = [] for i in range(COUNT_LIMIT):r = randint(0, 255)g = randint(0, 255)b = randint(0, 255)r = "%02x" % rg = "%02x" % gb = "%02x" % brandom_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 tkinter显示Mandelbrot图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个python网上文档
- 下一篇: tkinter绑定鼠标滚轮滚动事件