## Extending Fractals

So last time I posted about fractals, I gave you guys some code to render a monochrome Mandelbrot Set. Granted it’s cool, how about we throw some color into the mix?

As I explained in the last post, the mandelbrot set is colored if the iteration *z(c) *stays contained within *z(c) < 2*. So to color it, we will select appropriate hues based how quickly the complex point *(x, yi)* exceeds 2. Here’s the new *mandel(c)* method.

def mandel(c): z = 0 i = 0 for h in range(0,20): z = z**2 + c if abs(z) > 2: return cols[i / 4 - 1] i += 1 return "black"

What’s different about it? Rather than returning True or False, this method returns a color, based on how quickly the iteration becomes chaotic. Now, we will **always** draw a point, it just depends what color that point is.

for x in range(0,600): real = x / 200.0 - 1.5 for y in range(0,600): img = y / 200.0 - 1.5 c = complex(real, img) col = mandel(c) w.create_line(x,600-y,x+1,601-y,fill=col) w.pack()

And here’s the beautiful result…

Pretty straightforward, huh? Yeah, I thought so too. But let’s take this one step further, how about we tweak our code to accomodate all fractals in the Julia Set? To explain with this “Julia Set” thing is, think of squares and rectangles. A square is always a rectangle, but not all rectangles are squares. The Mandelbrot Set is a Julia Set, but not all Julia Sets are Mandelbrot Sets. You know what? It’ll become clearer once you see the code.

Now to render the Julia Set, we have to pass it two methods, a complex point* (x, yi)* and an equation. Each julia set depends on the equation you pass it, and this requires either a great amount of tweaking, or just looking up some cool patterns.

The Julia Set is determined the same way - equate ** z(c) = z_n-1(c)^2 + c**. Except, for Julia Sets, c remains constant throughout the test. This c is the equation you pass the method. This c determines what your fractal will look like. Let’s take a peak at some code. We’ll keep the same “colorizing” theme. t is the complex point, c is the static equation.

def julia(t, c): z = t i = 0 for h in range(0, iter): z = z**2 + c if abs(z) > 2: return cols[i / 10 - 1] i += 1 return "black"

Pretty straightforward. Except Julia sets produce some **amazing** figures. I’ll post my favorites below. But for now, here is the complete code. I’ve including some “favorite equations” so feel free to test em out.

# Julia Set generator by Jordan Scales # 12/02/2009 from Tkinter import * cols = ["orange", "yellow", "red", "blue", "white"] # favorites... # -0.8+0.156j # -0.70176-0.3842j # -0.4+0.6j # -0.835-0.2321j eq = -0.8+0.156j iter = 50 def julia(t, c): z = t i = 0 for h in range(0, iter): z = z**2 + c if abs(z) > 2: return cols[i / 10 - 1] i += 1 return "black" root = Tk() w = Canvas(root, width=600, height=600) w.pack() for hx in range(0, 600, 75): w.create_line(0,hx,600,hx,fill="blue") for hy in range(0, 600, 75): w.create_line(hy,0,hy,600,fill="blue") print 'Loading...' for x in range(0, 600): real = x / 200.0 - 1.5 for y in range(0, 600): img = y / 200.0 - 1.5 com = complex(real, img) col = julia(com, eq) w.create_line(x, 600-y, x+1, 600-y+1, fill=col) w.pack() print 'Done!' root.mainloop()

Now, I bet you wanna see some pictures, huh. No problem, I should really get this tatooed on my body.

Enjoy, and get experimenting! If I didn’t make anything clear, feel free to hit me up at prezjordan@ilictronix.com, or just drop me a tweet @ilictronix

/prez

38 notes