I wanted to try something different from the spiral figures I had been making earlier, and I thought it would be interesting to try simulating lightening. I initially wrote a branching function and extended it enough times to cover the canvas, but that felt like cheating, since it was limited to a particular image size. I decided instead to write a function that creates a single branch and then calls itself recursively. Since the zig-zagging of the lightning is produced with a pseudorandom element number generator, each image is different, and some of my initial efforts resulted in lightning that turned around and went back towards the sky. To fix that, I added a little if statement to keep things pointed downwards at all times. At first, I came up with stopping conditions that were based on any trace leaving the canvas, but I wasn’t happy with the resulting images - they would stop with many branches still “up in the air”. I like the idea of all the lightning making contact with the ground, so with a little help from Pythagoras and a recursion counter, I found something to reliably produce an image where the lightning stretches from sky to ground and the program doesn’t go into an infinite loop.
Here is the result:
I thought it would be nice to have the initial bolt start with a thick pen and become smaller as it approached the ground. I tried various approaches, and came up with the following:
I eventually realized that with a few extra lines of code, you can write vector-based .eps files, which seemed greatly preferable to taking screenshots of the Turtle Graphics window. For some reason, though, the canvas color is lost and replaced with white. So my white lightning on a black background resulted in a blank white image. No matter, Black lightning one a white background looks pretty cool as well: