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 decided it would be a fun exercise to port these example programs to javascript, since that seems to be the new cross platform language of choice in this decade. I tried to reproduce them as faithfully as possible without any attempt at further optimization, and I tried to use as few non-BASIC language constructs as possible. I say few as possible, since many BASIC language constructs don't have direct analogous constructs in javascript, for instance GOTOs and labels. So I present to you below, my implementation of these examples.
I ran into a few caveats during the porting process that are worth mentioning:
- To view my javascript source, just choose View Page Source and you should be able to look around and find the example code.
- 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.
- I tried to leave the BASIC code as comments amongst the javascript code so in case you're following along with the book you'll have some guide posts in the code.
- 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.
- The output from the programs for chapters 12 and 13 did not look like the diagrams in the book and I spent quite a bit of time debugging them looking for my error. In the end I downloaded the DOSBox emulator and a copy of QuickBASIC and ran the programs there. In both cases, the BASIC code ran but outputted exactly the same result that I got from my javascript version. So I can only conclude that there is an error either in the code or the diagram printed in the book.
- 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
No comments:
Post a Comment