miriam_e: from my drawing MoonGirl (Default)
miriam_e ([personal profile] miriam_e) wrote2026-01-28 08:05 am

L-systems in bash

Once again, I'm fiddling around with l-systems. The eventual aim is to develop a simple way to populate virtual worlds with plants. It has been many decades since I experimented with l-systems. This time I've begun with recreating the simple plant described in the Wikipedia l-systems page.

To give myself a baseline I wrote it in bash script. But bash doesn't have graphical output, so I used ImageMagick to draw the lines on a canvas, and displayed the result using the small, fast, image viewer feh. One nice thing about feh is that it can receive notification when the image it is displaying changes, and update its display. This let me watch the plant grow as the program proceeded.



It turns out that while bash is quite fast at generating the strings that define the shape, ImageMagick clogs things up somewhat. I asked ChatGPT about this and it surprised me by giving some great advice: ensure the canvas (which was in .png format) is not compressed and this will relieve a lot of the load. My computer still staggered under the weight, but it didn't do so badly. It took just over an hour to render the image below. My ancient CoCo computer from about 1983 took less than half that time. It had a 6809 microprocessor that ran at a feeble 0.89 MHz, whereas my current computer runs at an underwhelming 1.1 GHz (1,100 MHz) on each of 2 cores -- more than a thousand times faster. And if I run it on the XRoar CoCo emulator on my computer, but at full speed instead of the original speed, then it takes about 2 minutes.


L-system plant traced to the same level
of complexity on my old CoCo,


My next experiment (if I'm not distracted by some other shiny thing) will be to rewrite the program using awk, which will run much, much faster. For display I'll use the same technique as when I made Wolfram 1D cellular automata a while back. That is, write to the old ASCII .pbm image format. Since awk is primarily a text processing language, getting it to write image data to an image format that is intrinsically text is a marriage made in heaven. Also, I've already developed simple line-drawing, and other routines for awk. I'll still use feh to asynchronously display the image as it updates. This will be interesting.

It strikes me as weird that we have a great flowering of computer languages at the moment -- more than a thousand, last time I looked -- yet almost none of these can display or manipulate images or sound. It sometimes feels like we've gone backwards. We do have some specialised languages to manipulate images and sound, but they can do little else, and interfacing standard languages with those specialised ones can often be messy (like using ImageMagick to output graphics from other languages, such as bash).

It surprises me that with all the computing power we have today, progress feels like it is splintering. I can process images and sound and text in complex ways that once upon a time I could have only dreamed of, but in other ways it feels like we've hardly progressed. I used to be able to quickly whip up simple programs to process text and output graphics and sound... but no more, it seems. We have an amazing world that seems weirdly lopsided.

Additional: I just recompiled xRoar, the CoCo emulator, and got that warm feeling of being able to do those old experiments again, relatively easily and simply... though the CoCo's line editor is even worse than Linux/Unix's ancient ed line editor. The solution is to edit on linux and load the program into XRoar's CoCo.