2016-09-25 29 views
-1

我想寫一個mandelbrot設置爲python中的圖像,並且我的某個函數有問題。寫一個mandelbrot設置爲在python中的圖像

問題是:雖然我期望像this。我得到一個普通的白色圖像。這裏是我的代碼:

快速彙總代碼: 檢查值是否已設置,如果是,則在布爾值數組中標記爲true。然後,根據布爾數組繪製圖像,着色真實,並留下假的。

import math 
import numpy as np 
import scipy.misc as smp 
from PIL import PILLOW_VERSION 
from PIL import Image 

def iterate(x, y, iterationNum): 
    z = 0 
    coord = complex(x, y) 
    for a in xrange(iterationNum): 
     #Don't use fabs. It can be negative. 
     z = z * z + coord 
     #This is a comparison between complex and int. It probably won't work. 
     #You want |Z| which is: z.real ** 2 + z.imag ** 2 > 4 
     if math.fabs(z) > 2: 
      return False 
    return True 

def pixel(image,x,y,r,g,b): 
    """Place pixel at pos=(x,y) on image, with color=(r,g,b)""" 
    image.put("#%02x%02x%02x" % (r,g,b), (y, x)) 

#here's some example coloring code that may help: 
def draw(grid): 
    #Create a white image with the size of the grid as the number of pixels 
    img = Image.new('RGB', (len(grid), len(grid)), "white") 
    pixels = img.load() 
    for row in xrange(len(grid)): 
     for col in xrange(len(grid[row])): 
      if grid[row][col] == True: 
       #If that point is True (it's in the set), color it blue 
       pixels[row, col] = (0, 0, 255) 
    return img 

def mandelbrot(): 
    #you should probably use a square, it's easier to deal with 
    #The mandelbrot set fits completely within (-2, 2) and (2, -2) 
    #(-200, 200), (200, -200) is way too big! 
    TopLeftX = -2; BottomRightX = 2 
    TopLeftY = 2; BottomRightY = -2 
    #increment should be calculated based on the size of the bounds and the number of pixels 
    #For example, if you're between -2 and 2 on the X-Plane, and your image is 400 pixels wide 
    #Then your increment = (2 - (-2))/400 = 4/400 = .01 so that each pixel is 1/400th of the 
    #Total width of the bounding area 
    increment = 0.01 
    maxIt = 100 
    w = BottomRightX - TopLeftX 
    h = TopLeftY - BottomRightY 
    #This should be based on the size of the image, one spot in the area for one pixel 
    npArr = np.zeros((w/increment, h/increment), dtype=bool) 
    #Use the increment variable from above. It won't work with xrange because that doesn't 
    #Support decimals. You probably want to use a while loop or something 
    x = -2 
    y = 2 
    while TopLeftX <= x <= BottomRightX: 
     while TopLeftY <= y <= BottomRightY: 
      #I recommend using True or False in here (in the set or not) 
      #And then do your color calculations as I explained above 
      #Saves a lot of memory 
      if iterate(x, y, maxIt): 
       npArr[x, y] = True 
      y += increment 
    #once you've calculated the Trues and Falses, you'd call the draw() function 
    #using the npArr as the parameter. I haven't tested the code, so there may 
    #be a few bugs, but it should be helpful! 
     x += increment 
    return npArr 

img = draw(mandelbrot()) 
img.save("mandelbrot.png") 

我懷疑問題在於我的代碼中的「迭代」函數,因爲我放入迭代的值都沒有返回true。

編輯 我還有另一個問題,第二個for循環我在這裏甚至沒有運行。

+0

對於'iterationNum'的任何「truthy」值,'iteration'不可能返回'True',因爲它有一個只包含'return False'的無限循環。你的意思是做一些'for'循環或者可能改變循環中'iterationNum'的值? – smarx

+0

看起來你總是將'100'傳遞給'iteration',所以它確實只能返回'False'。 – smarx

+0

@smarx這就是我想要做的。 –

回答

0

您對y座標的處理有誤。你開始用

y = 2 

外循環,並具有循環條件爲

while TopLeftY <= y <= BottomRightY: 

取代它們的值之後,這是

while 2 <= y <= -2: 

這是一種無稽之談。接着是

y += increment  

但是y已經在該範圍的頂端。此外,您無法爲每個內部循環重置y

總之,循環應該是

x = TopLeftX        # use the value you already defined! 
while TopLeftX <= x <= BottomRightX: 
    y = TopLeftY       # moved to inside x loop 
    while TopLeftY >= y >= BottomRightY: # change the loop condition 
     # ... the Mandelbrot iteration 
     y -= increment      # reverse direction 
    x += increment 

我不是Python的專家,所以有可能是其他的問題了。

+0

非常感謝你的答案!我已經解決了你指出的所有問題,但我會讓你的答案是正確的。謝謝! :d –

相關問題