Last time we plotted a Mandelbrot set using a small Python script. This set is interesting because an unexpectedly large amount of detail emerges from the iteration of a relatively simple function. Even more interesting is the fact that this detail is not limited to any particular resolution. The Mandelbrot set exhibits fractal geometry, meaning that tiny areas of the set share features with the whole thing. If we zoom in, smaller details are visible no matter how far we choose to zoom.
But you don’t have to believe me – let’s modify our Python script and see what happens. Basically we have to map 600 pixels in each direction to arbitrary sections of the real and imaginary axes. We can have our script read in the desired ranges and calculate appropriate mappings. First, store the inputs in decimal variables:
def get_decimal(m): while True: try: x = float(input(m)) return x except ValueError: print('Enter a decimal number.')
loX = get_decimal('Minimum X: ') hiX = get_decimal('Maximum X: ') loY = get_decimal('Minimum Y: ') hiY = get_decimal('Maximum Y: ')
Divide 600 by the range in each direction to compute scale coefficients:
scaleX = 600.0/(hiX - loX) scaleY = 600.0/(hiY - loY)
Now modify the drawing code to use our designated ranges:
for x in range(0,600): real = loX + x / scaleX for y in range(0, 600): imag = loY + y / scaleY c = complex(real, imag) p = mandel(c) w.create_line(x, 600-y, x+1, 601-y, fill=colors[p]) w.pack()
With these changes we can zoom in and see some interesting features. Try using -0.15, -0.05, 0.9, and 1.0 as input values. More detail is visible, but it looks like some of the boundaries are smoothing out! Interestingly, that’s because our
mandel() function only checks whether each candidate
c escapes within 20 iterations. Points close to the boundary often take more than 20 iterations to escape, but they don’t actually belong in the set. Therefore, as the zoom level increases we have to test each candidate
c for more iterations in order to maintain an accurate image. This is left as an exercise for the reader.