使用Julia set进行平滑着色\标准化迭代 - Python

时间:2014-08-08 02:47:12

标签: python fractals mandelbrot

我一直在努力用python制作julia set代码。我没有编写原始代码,但我稍微修改了它,它实现了我想要的东西......除了光滑的着色!我已经阅读了如何做到这一点,但我仍然感到困惑。

代码预颜色平滑尝试:

def julia():
    while True:
        try:
            julwidth = int(input("What width would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            julheight = int(input("What height would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            iter = int(input("Max iterations? If you don't know, 255 is a good number \n"))
            break
        except ValueError:
            print("Enter an integer, please")
    print("Running Julia fractal... please wait...")
    imgx = julwidth
    imgy = julheight
    image = Image.new("RGB", (imgx, imgy))

    # drawing area
    xa = -2.0
    xb = 2.0
    ya = -1.5
    yb = 1.5
    maxIt = iter # max iterations allowed
    Time = time.clock()
    #print(time)
    # find a good Julia set point using the Mandelbrot set
    while True:
        cx = random.random() * (xb - xa) + xa
        cy = random.random() * (yb - ya) + ya
        c = cx + cy * 1j
        z = c
        for i in range(maxIt):
            if abs(z) > 2.0:
                break 
            z = z * z + c
        if i > 10 and i < 100:
            break

    # draw the Julia set
    for y in range(imgy):
        zy = y * (yb - ya) / (imgy - 1)  + ya
        for x in range(imgx):
            zx = x * (xb - xa) / (imgx - 1)  + xa
            z = zx + zy * 1j
            for i in range(maxIt):
                if abs(z) > 2.0:
                    break 
                z = z * z + c
            red = i % 8 *32
            green = i % 16 * 16
            blue = i % 32 * 8
                #print(red, green, blue)
            image.putpixel((x, y), (red, green, blue))
    print("Done computing. \n")

尝试平滑:

def julia():
    while True:
        try:
            julwidth = int(input("What width would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            julheight = int(input("What height would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            iter = int(input("Max iterations? If you don't know, 255 is a good number \n"))
            break
        except ValueError:
            print("Enter an integer, please")
    print("Running Julia fractal... please wait...")
    imgx = julwidth
    imgy = julheight
    image = Image.new("RGB", (imgx, imgy))

    # drawing area
    xa = -2.0
    xb = 2.0
    ya = -1.5
    yb = 1.5
    maxIt = iter # max iterations allowed
    Time = time.clock()
    #print(time)
    # find a good Julia set point using the Mandelbrot set
    while True:
        cx = random.random() * (xb - xa) + xa
        cy = random.random() * (yb - ya) + ya
        c = cx + cy * 1j
        z = c
        for i in range(maxIt):
            if abs(z) > 2.0:
                break 
            z = z * z + c
            smooth = (abs(z))
        if i > 10 and i < 100:
            break

    # draw the Julia set
    for y in range(imgy):
        zy = y * (yb - ya) / (imgy - 1)  + ya
        for x in range(imgx):
            zx = x * (xb - xa) / (imgx - 1)  + xa
            z = zx + zy * 1j
            for i in range(maxIt):
                if abs(z) > 2.0:
                    break 
                z = z * z + c
                smooth += (abs(z))
                #print(smooth)
            red = ceil(smooth % 8 * 32)
            green = ceil(red*red)
            blue = ceil(green*red)
                #print(red, green, blue)
            image.putpixel((x, y), (red, green, blue))
    print("Done computing. \n")

如果你不能说,我添加了一个名为smooth的变量,我用它来尝试通过使用复数/变量来平滑颜色,z 通过平滑尝试运行我的代码会产生一些东西......而不是我想要的东西。这是一个奇怪的蓝色图像,并不好。如何为此添加平滑?我可能不太了解平滑,所以解释可能会有所帮助。

1 个答案:

答案 0 :(得分:1)

计算小数转义计数并根据以下内容设置颜色:

ec = N + 1 - log (log |Z(N)|) / log 2其中N是转义'z'的值时的转义计数,Z(N)z转义时的值|z|z sqrt (x*x + y*y)的模数仅为{{1}}。