[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Draw a burning ship fractal with smooth colors in Python and tkinter

[A burning ship fractal drawn with smooth colors in tkinter and Python]

The example Draw the burning ship fractal in Python and tkinter iterates the following equation to generate the burning ship fractal.

Zn+1 = (|Re(Zn)| + |Im(Zn)| × i)2 + C

Where Zn and C are complex numbers. The program iterates this equation until the magnitude of Zn is at least 2 or the program performs a maximum number of iterations.

At that point, it uses the number of iterations to determine a color for the pixel. For example, if the program is using k colors and it performed i iterations, then it assigns the point color number i % k.

This example modifies the coloring algorithm to produce smoothly varying colors. First note that the following value mu approximates the fractional number of iterations that would be needed before the magnitude of Zn is at least 2.

mu = iteration + 1 - math.log(math.log(abs(z))) / log_escape

Here iteration is the number of iterations actually performed, abs(z) is the magnitude of Z right after the magnitude is greater than 2, and log_escape is the logarithm of the escape radius 2.

This value only approximates the actual expected fractional number of iterations and there is a noticeable error where the colors don't blend smoothly. Fortunately it's easy to reduce the error by using a later value of Zn. For example, if you use Zn + 3, then the error isn't noticeable. The following shows the code used by the program to calculate mu.

# Reduce the error in mu. for i in range(3): z = abs(z.real) + abs(z.imag) * 1j z = z * z + c iteration += 1 mu = iteration + 1 - math.log(math.log(abs(z))) / log_escape

[A burning ship fractal drawn with smooth colors in tkinter and Python] This program provides two smooth color models. If set color_style to smooth2, the program scales the resulting value of mu so it varies only once over the available colors as the number of iterations varies from 0 to the maximum number of iterations. That means the colors do not repeat and you get a more gradual smoothing. Because the more detailed areas of the fractal run to relatively large numbers of iterations, that also means much of the larger structure only uses the first color: red.

Finally after calculating mu with one of the color techniques, the code calls the following get_color method to return an appropriate color for the pixel.

def get_color(self, colors, mu): '''Get a color for this pixel.''' clr1 = int(mu) t2 = mu - clr1 t1 = 1 - t2 clr1 = clr1 % len(colors) clr2 = (clr1 + 1) % len(colors) r = int(colors[clr1][0] * t1 + colors[clr2][0] * t2) g = int(colors[clr1][1] * t1 + colors[clr2][1] * t2) b = int(colors[clr1][2] * t1 + colors[clr2][2] * t2) return (r, g, b)

This method truncates mu to find an integer color index value and a fractional value. It then returns a color that uses the weighted average of the integer color and the following color.

Download the example to experiment with it and to see additional details.

[A burning ship fractal drawn with smooth colors in tkinter and Python] [A burning ship fractal drawn with smooth colors in tkinter and Python] [A burning ship fractal drawn with smooth colors in tkinter and Python] [A burning ship fractal drawn with smooth colors in tkinter and Python] [A burning ship fractal drawn with smooth colors in tkinter and Python]
© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.