I stumbled on an interesting book on fractal algorithms: Chaos and Fractals: New Frontiers of Science. What made me take notice was that each chapter ended with a short, approachable example program that demonstrates a concept. The programs also generate some neat-looking fractal diagrams, and IMHO it's always fun to play around with algorithms that generate pictures.
Another interesting point was that, in the first edition of the book, each example program was written in the BASIC programming language. The choice of BASIC doesn't seem so odd with a little historical context. The book was published in 1992, and around that time there weren't very many good cross-platform programming languages, especially when you consider cross-platform drawing libraries. But, at the time, BASIC fit the bill. It could run on DOS-based PCs and Apple IIs, which, at the time, probably covered a pretty good swath of the audience for the book. Most BASIC dialects also included the LINE and PSET functions which were all the example programs needed to draw the output of all sorts of fractal algorithms. In hindsight, BASIC seems like a decent choice. In the second edition of the book, I believe the authors switched to something else like Java applets; I was working from a first edition copy from my local library so I don't know much about the second edition.
I ran into a few caveats during the porting process that are worth mentioning:
- Most of these algorithms are recursive and I had to get a little imaginative when converting GOTOs and GOSUBs to proper function calls.
- For displaying the graphical output of each program, I used the HTML canvas object.
- The example code uses several different invocations of the LINE and PSET functions, and I had to track down some BASIC language documentation to figure out what the invocations should do. As a result, it seems that the BASIC dialect used is closest to GW-BASIC and QuickBASIC.
- Some of the programs take a while to run (especially chapter 12 and 14), and this can lead modern browsers to display errors that the browser tab has hung or otherwise failed. In the chapter 12 code I tried to mitigate this somewhat by adding chunking code to sleep between chunks of loop iterations. It's less noticeable in chapter 14, but if you run into issues you can often get around it by just reloading the page and trying again.
Chapter 1: Graphical Iteration
Chapter 2: Sierpinski Gasket
Chapter 3: The Koch Curve
Chapter 4: The Cantor Set
Chapter 5: Iterating the MRCM
Chapter 6: Chaos Game for the Fern
Chapter 7: L-systems
Chapter 8: Cellular Automata
Chapter 9: Random Midpoint Displacement
Chapter 10: Times Series and Error Development
Chapter 11: Final State Diagram
Chapter 12: Rossler Attractor
Chapter 13: Julia Sets
Chapter 14: Mandelbrot Sets