Title: Draw the burning ship fractal in Python and tkinter
This program is very similar to the Mandelbrot set program described in the post Draw the Mandelbrot set and zoom in on areas in Python and tkinter. See that post for most of the details.
The big difference is that this program uses the absolute values of each point's real and imaginary parts when it iterates. Here's the equation that this program iterates.
Zn+1 = (|Re(Zn)| + |Im(Zn)| × i)2 + C
Where C = 0 and Z0 is the point.
Here's the Python code that calculates the pixels' colors. The change, which takes the absolute values of Zs real and imaginary parts, is hilighted in blue.
# Loop over the pixels.
for x in range(image_wid):
for y in range(image_hgt):
# Calculate this point's color.
re, im = self.d_to_w(x, y)
c = re + im * 1j
z = z0
iteration = 0
while ((iteration < max_iterations) and (abs(z) < escape_radius)):
# Calculate Z(clr).
z = abs(z.real) + abs(z.imag) * 1j
z = z * z + c
iteration += 1
# Set the pixel's color.
if (iteration >= max_iterations):
color = black
else:
color = colors[iteration % len(colors)]
self.pixels[x, y] = color
Download the example to experiment with it and to see additional details.
|