2017-05-10 59 views
0

我想使用python生成this等圖像的分形。我找到的代碼會生成正常的分形,我一直無法找到如何複製圖像分形的任何幫助。用於生成分形的代碼是 -使用python生成分形

from numpy import * 

def mandel(n, m, itermax, xmin, xmax, ymin, ymax): 

    ''' 
    (n, m) are the output image dimensions 
    itermax is the maximum number of iterations to do 
    xmin, xmax, ymin, ymax specify the region of the 
    set to compute. 
    ''' 

    ix, iy = mgrid[0:n, 0:m] 
    x = linspace(xmin, xmax, n)[ix] 
    y = linspace(ymin, ymax, m)[iy] 
    c = x+complex(0,1)*y 
    del x, y 
    img = zeros(c.shape, dtype=int) 
    ix.shape = n*m 
    iy.shape = n*m 
    c.shape = n*m 
    z = copy(c) 
    for i in xrange(itermax): 
     if not len(z): 
      break 
     multiply(z, z, z) 
     add(z, c, z) 
     rem = abs(z)>2.0 
     img[ix[rem], iy[rem]] = i+1 
     rem = -rem 
     z = z[rem] 
     ix, iy = ix[rem], iy[rem] 
     c = c[rem] 
    return img 

if __name__=='__main__': 
    from pylab import * 
    import time 
    start = time.time() 
    I = mandel(512, 512, 100, -2, .5, -1.25, 1.25) 
    print 'Time taken:', time.time()-start 
    I[I==0] = 101 
    img = imshow(I.T, origin='lower left') 
    img.write_png('../images/mandel.png') 
    show() 

我需要知道如何使用圍繞其構建分形的基本圖像。有人可以請指點我正確的方向嗎?

回答

0

它被稱爲軌道陷阱。基本上,在循環內你有當前的軌道值z。對於軌道中的每個值(因此,z的每個值),檢查該值是否與圖像內的某個座標相對應。如果是,則返回該像素的相應顏色。其餘部分與正常逃逸時間算法完全相同。我使用C++類似的僞代碼來說明它的最簡單情況:scale(int x,int y)是一個返回與x,y的座標對應的複數的函數,unscale是它的計數器部分。

int getcolor(int x, int y, int maxiter) { 
    complex z = 0; 
    complex c = scaled(x, y) 
    for(int i = 0; i < maxiter; ++i) { 
     z = z * z + c; 

     // these 3 lines are where the magic happens 
     if(unscale(z) is within image bounds) { 
      color = image[unscale(z)]; 
      return color; 
     } 

     if(abs(z) > 2) return color based on i; 
    } 
    return 0; // lake 
} 

我不能真正按照你所有的列表解析Python代碼。

一旦你運行了,有很多事情要發現和修改。維基百科有一些更一般的信息https://en.wikipedia.org/wiki/Orbit_trap 此外,這個博客可能會給你更多的見解:http://iquilezles.org/www/articles/ftrapsbitmap/ftrapsbitmap.htm