[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[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: Interactively set anchors for the chaos game to draw strange attractor fractals in Python

[A strange attractor made by the chaos game in Python] My post Use the chaos game to draw strange attractor fractals in Python explains how you can use the chaos game to draw strange attractor fractals. That example positioned anchor points on the vertices of regular polygons, but you can also get interesting results from anchors that are not arranged evenly. This example lets you click to define the anchor points.

You may remember from the previous post that a strange attractor is a fractal produced by a chaotic system that generates points that are attracted to some sort of result. The points don't repeat, but they are inescapably drawn to a shape of some sort that is often vaguely defined or even with a ghostly appearance. The images created by this example and the previous one are strange attractors.

Defining Anchors

This example really only has a few very small changes from the previous version.

First, of you click on the program's canvas, the following event handler executes.

def mouse_down(self, event): '''Define a new anchor.''' self.anchors.append((event.x, event.y)) self.clear() self.enable_buttons()

This code adds the clicked point to the self.anchors list. It then clears the current drawing and calls enable_buttons, which is described shortly.

When you click Delete Anchors, the following code executes.

def delete_anchors(self): '''Delete all anchors.''' self.anchors = [] self.clear() self.enable_buttons()

This code creates a new empty self.anchors list, clears the drawing, and calls enable_buttons.

Here's the enable_buttons method.

def enable_buttons(self): '''Enable or disable buttons.''' self.drawing = False self.start_button.config(text='Start') if len(self.anchors) > 2: self.start_button.config(state=tk.NORMAL) else: self.start_button.config(state=tk.DISABLED)

This code sets self.drawing = False to stop the program from drawing further. It changes the text on the Start/Stop button to Start because drawing is currently stopped.

If the number of anchors is greater than 2, the code enables the Start/Stop button. If the number of anchors is 0, 1, or 2, it disables the button. Drawing with no anchors would cause an error. Drawing with 1 or 2 anchors is pretty boring. (Try it and see.)

Conclusion

Those are the program's key pieces. I've skipped a bunch of details, but they're straightforward so I'm sure you can figure them out by looking at the code. Download the program to see those details.

[A strange attractor made by the chaos game with five anchors in Python] [A strange attractor made by the chaos game with five anchors in Python] [A strange attractor made by the chaos game with six anchors in Python]

[A Sierpinski pyramid fractal] The picture on the left is a three-dimensional version of the Sierpinski gasket called the Sierpinski pyramid.

This picture was generated by techniques described in my book Build Your Own Ray Tracer With Python.

[Build Your Own Ray Tracer With Python]
© 2024 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.