## Fractal Function Source Code

In Chapter 9 I made use of a variation of the fractal script in Chapter 7 to create a collage by wrapping it within a function. Listing A-5 shows the function used in creating the fractal collage in Chapter 9.

Listing A-5. Fractal Collage Function from PIL import Image from cmath import *

def fractal(delta=0.000001, res=800, iters=30):

Creates a z**4+1=0 fractal using the Newton-Raphson method

# create an image to draw on, paint it black img = Image.new("RGB", (res, res), (0, 0, 0))

# these are the solutions to the equation z**4+1=0 (Euler's formula) solutions = [cos((2*n+1)*pi/4)+1j*sin((2*n+1)*pi/4) for n in range(4)] colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0)]

for re in range(0, res): for im in range(0, res): z = (re+1j*im)/res for i in range(iters): try:

# possibly divide by zero exception continue if(abs(z**4+1) < delta): break

# color depth is a function of the number of iterations color_depth = int((iters-i)*255.0/iters)

# find to which solution this guess converged to err = [abs(z-root) for root in solutions] distances = zip(err, range(len(colors)))

# select the color associated with the solution color = [i*color_depth for i in colors[min(distances)]] img.putpixel((re, im), tuple(color))

return img