Sierpinski Triangles, Part Two
OK, just a few days late. Such is life.
I have still been playing with these fractal triangles, but not in your average programming language. Work’s been pretty busy, so lunchtime Scheme has taken a back seat to cleverly disguising the fact that I’m eating a sandwich while on the phone by pretending it’s a bad line. So, my limited evening space has been used to throw together a triangle using a tool that pretty much everybody has on their computer, but probably has no idea what it is. I’m talking about XSLT, that bastard child of the XML revolution.
XSLT, or eXtensible Stylesheet Language Transformations, are a method of turning one XML document into another XML document. That may seem about as useless as giving Tim a book on Ruby, but there are lots of different XML documents out there. I’ve used it before to transform XSD and WSDL files into XHTML, the form of XML a lot of us are actually used to. I also hear people build proper websites with it.
As I’m more interested in the technology than actual facts, I’m going to use Wikipedia for research here. Apparently, the language’s design is heavily influenced by functional languages, and “text-based pattern matching languages like SNOBAL and awk”. I’m not entirely how much of that you should take without salt, but I do know it’s powerful once you’re used to it. Oddly, it’s one of those technologies that Internet Explorer implemented before anyone else, although it’s first attempt rather far removed the actual specification of the format.
In this particular case, I had the brain wave that, you know what also is an XML based format? SVG! Yes! Now, can XSLT do recursion? It can? Hell yeah! In fact, a little research reveals that XSLT is fucking awesome. It’s like html on crack, really. Some playing around built a working model, and some further research revealed somewhere in the region of 2000 other people have had the idea before me.
Odds are on you’d like to see this baby. Or not. But trust me, it’s pretty cool! Grab this file, and this one. DO NOT LEFT CLICK THEM! Save them. On your desktop, home directory, whatever. Then, load the XML file in a convenient text editor. The ‘depth’ option is the crucial one here. ‘3′ loads in a second on my computer, ‘5′ takes a few moments, ‘10′ took enough time for me to get another beer… looked damn cool when finished though! To be honest, anything above 9 won’t yield much of an improvement. Anyway, to actually view the triangle of doom open the XML file in Firefox. It should find the stylesheet, generate an SVG file, and display it. IE can do it too, but it gets confused by SVG. Close the window once your done marveling, ’cause it’ll eats memory like an elephant eats peanuts.
It’s pretty cool, but it’s not great. The algorithm is pretty memory hungry, and it’s certainly not an L-System. That’s kinda my holy grail, but I think I’ll share this with the world:

What it says is: ‘Free meal to the first person to draw a sierpinski triangle using Lisp, Zelda ML, Scheme, VB, Python, Ruby, BrainFuck, C++, Perl, Java’. Basically, I’m offering to cook (or purchase, depending on the person. Work that one out, bitches) a meal for the first person to knock one of these babies up, in any language. In theory, it’s not particularly difficult, but it’s quite a cool little challenge involving recursion. Enjoy!
As a sideline, I’m altering the last bit. Instead of ‘BONUS POINTS: Draw one using XSLT & SVG or Postscript’, I’ll give a pint to the person who builds a more efficient triangle than mine using XSLT. The Postscript bit is covered in the first challenge.
PS: Ben raised the point that he’d like to do it in Lisp (or whatever it was, I was drunk at the time), but he doesn’t know any drawing libraries for the language. Well, on one hand learning a new library is always informative. On the other, knock together a quick function that draws lines using asterisks and bob’s your uncle.
Food is on offer here people! Hop to it!

