zoukankan      html  css  js  c++  java
  • 转 用Python画Mandelbrot集

    照着oldj同学的方法画曼德勃罗集,需要PIL库的支持,我生成的图是1280×800,迭代了1024次,花费了20多分钟。效果如下:

      代码如下:

     1 # -*- coding: utf-8 -*-
     2 import time
     3 import Image, ImageDraw
     4  
     5 g_size = (1280, 800) # 图形最终尺寸
     6 g_max_iteration = 1024 # 最大迭代次数
     7 g_bailout = 4 # 最大域
     8 g_zoom = 2.5 / g_size[0] # 缩放参数
     9 g_offset = (-g_size[0] * 0.25, 0) # 偏移量
    10 g_HSL = (210, 80, 50) # HSL色彩基调
    11  
    12 def draw(antialias = True):
    13     zi = 2 if antialias else 1 # antialias: 抗锯齿 size = [i * zi
    14     size = [i * zi for i in g_size]
    15     zoom = g_zoom / zi
    16     offset = [i * zi  for i in g_offset]
    17     bailout = g_bailout * zi
    18     img = Image.new("RGB", size, 0xffffff)
    19     dr = ImageDraw.Draw(img)
    20  
    21     print "painting Mandelbrot Set.."
    22     for xy, color in getPoints(size, offset, zoom):
    23         dr.point(xy, fill = color)
    24     print "100%\n"
    25  
    26     del dr
    27     if antialias:
    28         img = img.resize(g_size, Image.ANTIALIAS)
    29     img.show()
    30     img.save("mandelbrot_set_%dx%d.png" % g_size)
    31  
    32 def getPoints(size, offset, zoom, ti = 0, tstep = 1):
    33     "生成需要绘制的点的坐标及颜色"
    34  
    35     def getRepeats(c):
    36         z = c
    37         repeats = 0
    38         while abs(z) < g_bailout and repeats < g_max_iteration:
    39             z = z * z + c
    40             repeats += 1
    41         return repeats
    42  
    43     def getColor(r):
    44         color = "hsl(0, 0%, 0%)"
    45         if r < g_max_iteration:
    46             v = 1.0 * r / g_max_iteration
    47             h = ch * (1 - v)
    48             s = cs
    49             l = cl * (1 + v)
    50             color = "hsl(%d, %d%%, %d%%)" % (h, s, l)
    51         return color
    52  
    53     xs, ys = size
    54     xw, yh = xs / 2, ys / 2
    55     xo, yo = offset
    56     ch, cs, cl = g_HSL
    57  
    58     progress = 0
    59     for iy in xrange(ys):
    60         p = iy * 100 / ys
    61         if iy % 10 == 0 and p != progress:
    62             print ("%d%%..." % p) # 显示进度
    63             progress = p
    64         for ix in xrange(ti, xs, tstep):
    65             x = (ix - xw + xo) * zoom
    66             y = (iy - yh + yo) * zoom
    67             c = complex(x, y)
    68             r = getRepeats(c)
    69             yield (ix, iy), getColor(r)
    70  
    71 def main():
    72     t0 = time.time()
    73     draw()
    74     t = time.time() - t0
    75     print "%dm%.3fs" % (t / 60, t % 60)
    76  
    77 if __name__ == "__main__":
    78     main()
  • 相关阅读:
    ios 应用程序之间的跳转(内置程序的实现)
    iOS手势UIGustureRecognizer
    动画学习之Animating Views with Blocks
    get 和post 方式请求数据
    通过路径添加图片
    contentSize、contentInset和contentOffset区别
    bzoj1055 [HAOI2008]玩具取名 区间DP
    bzoj1025 [SCOI2009]游戏 动态规划
    POJ2299 树状数组求逆序对
    洛谷1525 关押罪犯NOIP2010 并查集
  • 原文地址:https://www.cnblogs.com/lkprof/p/3118667.html
Copyright © 2011-2022 走看看