【问题标题】:Generate fractals with python用python生成分形
【发布时间】:2017-05-10 07:58:59
【问题描述】:

我想使用 python 生成像this 这样的图像的分形。我发现的代码会生成正常的分形,我一直无法找到有关如何复制图像的分形的任何帮助。用于生成分形的代码是 -

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()

我需要知道如何使用构建分形的基础图像。有人可以指出我正确的方向吗?

【问题讨论】:

    标签: python fractals


    【解决方案1】:

    它被称为轨道陷阱。基本上,在循环内你有当前的轨道值 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

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2012-05-10
      相关资源
      最近更新 更多